Numerical input should not exceed 8 digits ( counting numbers on both the side of the decimal). Example: 123456.78 or 0.12 or 1.23
Numerical values should be able to accept negative numbers. Example: -0.8 , or -1.23
Max of 2 decimal number allowed
Numerical value should not accept any alpahbets in the input box
I tried Creating the following regular expression but i am not able to figure it out quite efficiently
^-?[0-9]\d*(\.\d+)?[,8]$
Regular exp
To match the required number of digits and no more, lookahead at the beginning of the pattern for (?:\d\.?) 1 to 8 times, and then match \d*(?:\.\d{1,2})?$ to match a number which, if containing decimals, contains at maximum 2 decimal characters:
^-?(?=(?:\d\.?){1,8}$)\d*(?:\.\d{1,2})?$
https://regex101.com/r/rQMRVX/5
(unless you need to capture the decimal part, it can be a non-capturing group like above)
You can try this
^-?\d{1,8}.?\d{1,2}$
Link : https://regex101.com/r/x7yw5M/2
Related
I am trying to build regex that can validate number or range input.
Allowed values are
Any number between 1 and 1816
A range consisting of 2 numbers separated by -. Each number must be between 1 and 1816. E.g. "1-1816", "3-100", "1815-1816"
Invalid values include
"0"
negative numbers (like "-13")
numbers with leading zeros (like "01")
numbers out of range 1-1816 (alone or as part of range)
Any regex will be fine JavaScript or C#.
So far I figured out just
(?<=\s|^)\d+(?=\s|$)
The problem with your regex is that you are accepting all digits via \d and not limiting it to suit your accepted range.
Use this:
^(?:181[0-6]|180\d|1[0-7]\d{2}|[1-9]|\d{2,3})(?:-(?:181[0-6]|180\d|1[0-7]\d{2}|[1-9]|\d{2,3}))?$
This regex limits the numbers to be in the range 1-1816, while supporting either individual numbers or a range via a hyphen separator as specified.
Demo
This regex should do the trick:
^([1-9][0-9]{0,2}|1[0-7][0-9]{2}|180[0-9]|181[0-6])(-([1-9][0-9]{0,2}|1[0-7][0-9]{2}|180[0-9]|181[0-6]))?$
Basically it allows
A number with leading 1 and 0 to 2 more digits
A number with leading 1, followed by 0-7 and twice any number
A number with leading 180 followed by one more number
A number with leading 181 followed by one number 0-6
This optionally once more repeated after -
But generally regexes are not good to work with numbers (and hard to update whne the number changes). If you have such option I'd rather just validate the number by much simpler regex, like
^[1-9][0-9]{0,3}(-([1-9][0-9]{0,3}))?$
and than programmatically split it by - (if present) and parse the individual segments as integer and validate their numeric value against the range.
That would additionally allow you to check for invalid ranges like 100-1, 1800-1800 etc.
With reference to my previous question currency regex
I want to add an condition, i want to allow decimal only if it starts with 0
example
0.25 should be allowed
1.25 not allowed
current regex is as following
/^(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$/
which matches comma trailing etc.
This will match all the currency matches from before without decimals or decimals with 0.x*
/^((?:\d{1,3}(?:,\d{3})+|\d+)|(?:0\.\d+))$/
If you only want to match 0.xx instead of an arbitrary number of decimal places use
/^((?:\d{1,3}(?:,\d{3})+|\d+)|(?:0\.\d{2}))$/
This one changes \d+, one or more digits, to \d{2}, exactly 2 digits.
Could any of you help me with a regular expression which will accept these:
For every thousand a comma will be accepted.
No less than 4 decimal points in number
I've been testing this pattern but fails in some scenarios:
^\d+(\,\d+{1.3} \.d{1.4})*$
Valid inputs:
1
11
111
1,111
1,111,111
1.1111
11.1111
111.1111
1,111.1111
Invalid inputs:
Any letter
1,1
1.1
1.11
1,11.11
1,111.1
The main criterions are:
(4 decimal points are obligatory).
(3 numbers per thousand must have a comma.)
Is this more complicated than
/^\d\d?\d?(,\d\d\d)*(\.\d\d\d\d)?$/
or if you prefer
/^\d{1,3}(,\d{3})*(\.\d{4})?$/
I need a little help with changing the following regex to allow negative numbers. It needs to allow decimal numbers of up to a maximum of 4 decimal places, which it already does.
/^\d+(\.\d{1,4})?$/
Can anyone help?
Thanks
Just put -? before the first digit - "optionally match a minus sign"
Add the - in the character class with optional(?)
/^[-+]?\d+(\.\d{1,4})?$/
The following allows for integer numbers (123), integer with 1 to 4 fractional part (123.1234) and only 1 to 4 fractional part (.1234), that's usually accepted as a valid number. All options can be prefixed by plus or minus signs:
/^[-+]?(?:\d+|\d*(?:\.\d{1,4})?)$/
The (?:xxx) construction is preferred to (xxx) when we're not interested to capture the matches, because it requires less resources.
I found this: Regex to match digits of specific length but it talks about Python. I am wanting to be able to get a group of random numbers of specific length. So if I have 167691#15316243 it will match 15316243. I am not sure how to implement this. right now I have new RegExp('[0-9]+', "g"); which matches a group of numbers fine, but now I realized I will have some times when I have more than one group and I only want the group of eight numbers.
[0-9]+ - Matches one or more numbers
[0-9]{8} - Matches exactly 8 numbers.
[0-9]{8,10} - Matches between 8 and 10 numbers.
[0-9]{8,} - Matches 8 or more numbers.
[0-9]* - Matches zero or more numbers.
[0-9]? - Matches zero or one number.
You can specify the length of a matching set using {}.
For example: [0-9]{8}
Which will match any numbers from 0 to 9 with a specific length of 8 characters.
You can also specify a min/max range instead of forcing a specific legnth. So if you wanted a min of 4 and a max of 8 the example would change to: [0-9]{4,8}
Simply put the repetition amount in curly braces:
"167691#15316243".match(/\d{8}/g);
Here's the fiddle: http://jsfiddle.net/3r5vd/
I'd suggest you read this article (scroll down to the section on Limiting Repetition).
Here's a quote:
Modern regex flavors [...] have an additional repetition operator that allows you to specify how many times a token can be repeated. The syntax is {min,max}, where min is a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite. So {0,} is the same as *, and {1,} is the same as +.
Omitting both the comma and max tells the engine to repeat the token exactly min times.