Validating input using regular expression in Javascript
I need to validate text box, it should accept only alphabets (capital or small), digits, and .,?-_
.
It should not accept any other values.
It shoul开发者_如何学编程d not accept same value like ......
or ,,,,,
or -----
or ????
or _____
.
It should not accept values like ,._
it should contain either alphabet or digit with this, like eg _.ab.?
.
So you want the string to contain at least one (ASCII) alphanumeric character and at least two different characters?
Try
/^(?=.*[A-Z0-9])(?!(.)\1*$)[A-Z0-9.,?_-]*$/i
Explanation:
^ # start of string
(?=.*[A-Z0-9]) # assert that there is at least one of A-Z or 0-9
(?!(.)\1*$) # assert that the string doesn't consist of identical characters
[A-Z0-9.,?_-]* # match only allowed characters
$ # until the end of the string
精彩评论