简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM