我需要创建一个匹配此格式的正则表达式:B001169875
有人可以帮助我吗?
尝试使用jQuery Validate自定义方法使其工作:
jQuery.validator.addMethod("brandnumber", function(value, element) {
return /B\d{9}/m.test(value);
}, "Account number must start with B and be followed by 9 numbers.");
答案 0 :(得分:5)
您应该指定有关格式的更多详细信息。但它将是:
[A-Z][0-9]{9}
表示:一个大写字母(A..Z),然后是九位数(0..9)。
答案 1 :(得分:5)
你说字符串总是以B
开头,后跟数字。正则表达式取决于您想要匹配的数量:
此外,您可能必须使用^
和$
来指定字符串应仅包含B和数字:/^B\d{9}$/
,/^B\d*$/
或{{1} }。
答案 2 :(得分:2)
如果始终以B
开头,后面跟着9个数字,那么这样就可以了:
^B\d{9}$
你可以像这样使用它(取决于你想如何使用它):
jQuery.validator.addMethod("brandnumber",
function(value, element) {
return /^B\d{9}$/m.test(value);
},
"Account number must start with B and be followed by 9 numbers."
);