我尝试为仅接受字母、数字、破折号和下划线的特定字段添加规则,但规则中不包含破折号。问题是,如何使用以下格式将破折号添加到字符中。
jQuery.validator.addMethod("custom-rule", function(value, element) {
return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, dashes and underscores only please");
使用字符类在正则表达式中添加破折号和单词字符。
jQuery.validator.addMethod("custom-rule", function(value, element) {
return this.optional(element) || /^[\w-]+$/i.test(value);
// here add dash in character class-^^^^-----
}, "Letters, numbers, dashes and underscores only please");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句