[英]Regular expression for numbers with one hyphen(1-5) and numbers with single spaces (1 2 3 4 5)
[英]Regular expression which allows numbers, spaces, plus sign, hyphen and brackets
我正在使用jquery验证器,我在其中添加了一个方法来验证字符串,该字符串只允许数字,空格,加号,连字符和括号。 数字在字符串中是必需的,但其他租约者是可选的。
我在jquery validor中添加方法的代码:
jQuery.validator.addMethod( "regex", function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
遵循规则的代码:
rules: {
myfield: {
required: true,
regex: "[0-9]+" // want to add regular expression but I wrote only for digit which works but do not understand how to reach at my requirements.
},
}
您可以将所需字符添加到字符类中
/^(?=.*[0-9])[- +()0-9]+$/
正则表达式解释
(?=.*[0-9])
正面向前看。 确保至少有一位数
[- +()0-9]+
匹配数字,空格,加号,连字符和括号
要么
如果你不愿意使用展望未来。 你可以在没有它们的情况下写一个lenghier正则表达式
/^[- +()]*[0-9][- +()0-9]*$/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.