I need to validate text box, it should accept only alphabets (capital or small), digits, and .,?-_.
It should not accept any other values.
It should 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.?.
-
Can you describe what the field represents? That would make it clearer what logic you are trying to capture.Michael Aaron Safyan– Michael Aaron Safyan2011-02-25 09:56:57 +00:00Commented Feb 25, 2011 at 9:56
-
Please show some specific code you've tried. If you don't know where to start, google "javascript regex tutorial"Matt– Matt2011-02-25 09:57:41 +00:00Commented Feb 25, 2011 at 9:57
-
Related to you question, I hope you know that you in any case also MUST do the same input validation server side as well, no matter how perfect your javascript is doing it.hlovdal– hlovdal2011-02-25 12:38:14 +00:00Commented Feb 25, 2011 at 12:38
Add a comment
|
1 Answer
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