Regex for validate string of int range - javascript

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.

Related

How to create a Regular Expression with the following conditions

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

Regex for sting upto 24 characters limit, but should have atleast 10-16 numbers

i am trying to come up with a regex expression. The user can enter a string from with minimum 10 characters and maximum 24 characters, but the string should contain 10 digits and can have a maximum of 16 digits in it.
can someone help. I tried using (?=[0-9]{10,16}) (?=.{10,24})$
One possible approach:
/^(?=.{10,24}$)\D*(?:\d\D*){10,16}$/
The lookahead expression checks the string length, the one after checks the number of digits.
Here's the demo; I've decreased the limits quite a bit for obvious reasons.
The key difference with your pattern is usage of \D*(?:\d\D*) expression; without it only the direct sequence of digits will be matched.
You could always use string match for /[0-9]/g and calc the length of the returned array.
> '93k03k30dk30483'.match(/[0-9]/g).length
11

currency regex with decimal allowed with 0

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.

Regex to allow negative values with maximum of 4 decimal places

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.

Regex to match a specific group of digits of certain length?

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.

Categories

Resources