I have input field which takes 12 digits number. I want to throw error when user enters 12 digits same number. Atleast one number has to be different.
E.g
111111111111 - Error
111111111112 - Ok
123456789012 - Ok
I tried this (but i want inverse of the specified regex )
var pattern = "^([0-9])\\1{3}$";
var str = "5555";
pattern = new RegExp(pattern);
if(!pattern.test(str))
{
alert('Error');
}
else
{
alert('Valid');
}
code from : https://stackoverflow.com/a/2884414/1169180
Fiddle : http://jsfiddle.net/wn9scv3m/10/
Edit: No manipulation in if(!pattern.test(str))
in this line is allowed
You can use this regex:
^(\d)(?!\1+$)\d{11}$
Explanation:
^
- Match line start(\d)
- match first digit and capture it in back reference #1 i.e. \1
(?!..)
is a negative lookahead(?!\1+$)
means disallow the match if first digit is followed by same digit (captured group) till end.\d{11}$
match next 11 digit followed by line endIf 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