Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中使用正则表达式验证输入_Javascript_Regex_Validation - Fatal编程技术网

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

在Javascript中使用正则表达式验证输入,javascript,regex,validation,Javascript,Regex,Validation,我需要验证文本框,它应该只接受字母(大写或小写)、数字和、-。 它不应接受任何其他值。 它不应接受相同的值,如…或,,,,或-或??或。 它不应该接受像,。\u这样的值。它应该包含字母或数字,比如.ab.?,所以您希望字符串包含至少一个(ASCII)字母数字字符和至少两个不同的字符 试一试 说明: ^ # start of string (?=.*[A-Z0-9]) # assert that there is at least one of A-Z or 0-

我需要验证文本框,它应该只接受字母(大写或小写)、数字和
、-
。 它不应接受任何其他值。 它不应接受相同的值,如
,,,,
-
??

它不应该接受像
,。\u
这样的值。它应该包含字母或数字,比如
.ab.?
,所以您希望字符串包含至少一个(ASCII)字母数字字符和至少两个不同的字符

试一试

说明:

^                # 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

你能描述一下这个字段代表什么吗?这将使您更清楚地了解您试图捕获的逻辑。请显示一些您尝试过的特定代码。如果你不知道从哪里开始,谷歌的“javascript正则表达式教程”与你的问题有关,我希望你知道,在任何情况下,你也必须做同样的输入验证服务器端,无论你的javascript做得多么完美。
^                # 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