开发者

Validating input using regular expression in Javascript

开发者 https://www.devze.com 2023-02-13 22:18 出处:网络
I need to validate text box, it should accept only alphabets (capital or small), digits, and .,?-_. It should not accept any other values.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消