我有一个7位数长度的变量"CustCd“。第一个数字可以是数字或字母,但最后6个数字(位置2-7)必须是数字。我正在尝试使用一个regex where pattern = "0-9 A-Z a-z{1} 0-9{2,7}“与类validate,但它不工作。
下面是我的代码:
<input type="text" name="CustCd" id="CustCd"
size="7" maxlength="7"
pattern="[0-9 A-Z a-z]{1} [0-9]{2,7}"
title="Customer Code" class="validate required" />这是我第一次使用正则表达式,所以我想我可能忽略了一些东西。非常感谢您的帮助。
发布于 2015-07-09 00:36:18
尝试使用^[a-zA-Z0-9]{1}[0-9]{6}$作为您的模式。请记住,此验证是针对JavaScript regEx引擎进行的。
<input type="text" name="CustCd" id="CustCd" size="7" maxlength="7"
pattern="^[a-zA-Z0-9]{1}[0-9]{6}$" title="Customer Code" class="validate required" />还要确保页面上有正确的文档类型
<!DOCTYPE html>通过regex101.com进行解释
^ assert position at start of the string
[a-zA-Z0-9]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
[0-9]{6} match a single character present in the list below
Quantifier: {6} Exactly 6 times
0-9 a single character in the range between 0 and 9
$ assert position at end of the string可以尝试的完整示例代码
<!DOCTYPE html>
<form>
<input type="text" name="CustCd" id="CustCd" size="7" maxlength="7" pattern="[a-zA-Z0-9]{1}[0-9]{6}" title="Customer Code" class="validate required" />
<input type="submit">
</form>https://stackoverflow.com/questions/31296842
复制相似问题