vijuuuu
vijuuuu

Reputation: 9

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 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.?.

Upvotes: 0

Views: 1025

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336078

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

Upvotes: 2

Related Questions