I am trying to validate a comma separated list of numbers 1-31 unique (not repeating).
i.e.
2,4,6,7,1 is valid input.
2,2,6 is invalid
2 is valid
2, is invalid
1,2,3,4,15,6,7,31 is valid
1,2,3,4,15,6,7,32 is invalid
20,15,3
I tried
^((([0]?[1-9])|([1-2][0-9])|(3[01]))(?!([0]?[1-9])|([1-2][0-9])|(3[01])*,\\1(?!([0]?[1-9])|([1-2][0-9])|(3[01])) but it's accepting repeating numbers
For a number range that exceeds 1 digit, just add word boundary's around
the capture group and the back reference.
This isolates a complete number.
This particular one is numb range 1-31
^ # BOS
(?! # Validate no dups
.*
( # (1 start)
\b
(?: [1-9] | [1-2] \d | 3 [0-1] ) # number range 1-31
\b
) # (1 end)
.*
\b \1 \b
)
(?: [1-9] | [1-2] \d | 3 [0-1] ) # Unrolled-loop, match 1 to many numb's
(?: # in the number range 1-31
,
(?: [1-9] | [1-2] \d | 3 [0-1] )
)*
$ # EOS
var data = [
'2,4,6,7,1',
'2,2,6',
'2,30,16,3',
'2,',
'1,2,3,2',
'1,2,2,3',
'1,2,3,4,5,6,7,8'
];
data.forEach(function(str) {
document.write(str + ' gives ' + /^(?!.*(\b(?:[1-9]|[1-2]\d|3[0-1])\b).*\b\1\b)(?:[1-9]|[1-2]\d|3[0-1])(?:,(?:[1-9]|[1-2]\d|3[0-1]))*$/.test(str) + '<br/>');
});
I have created a pattern that can do this.
The pattern:^((?!(\d+),[^\n]*\b\2\b)([1-9]\b|[1-2]\d|3[0-1])(,(?1))?)$
A demo.
A short description.
^ - matches start of a line(?!(\d+),[^\n]*\b\2\b) - Looks ahead to ensure the next number is not repeated
(\d+) - grab next number,[^\n]* - a comma followed by anything but a new line\b\2\b - The next number again repeated([1-9]\b|[1-2]\d|3[0-1]) - Checks next number between 1-31
[1-9]\b - Checks for single digit. Boundary used so to prevent two digit numbers matching.[1-2]\d - Checks for 10-293[0-1] - Checks for 30,31(,(?1))?) If followed by comma recurse on main pattern to check if next number is repeated., - checks followed by acomma(?1) - recurses on main pattern.$ - End of line Updated: Forgot to check 1-31
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With