简体   繁体   English

在Javascript中使用正则表达式验证输入

[英]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.? 它不应该接受像这样的值,._它应该包含字母或数字,例如_.ab.? .

So you want the string to contain at least one (ASCII) alphanumeric character and at least two different characters? 所以你希望字符串包含至少一个(ASCII)字母数字字符和至少两个不同的字符?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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