$.validator.addMethod(
"regex",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
$.validator.addClassRules({
link:{
required: true,
regex: "(https?:\/\/(www\.))?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"
},
});
我认为正则表达式打破了字符串顶点,因此它不起作用。我该如何解决这个问题?
答案 0 :(得分:1)
jQuery.validator.addMethod('alpha_numeric', function(value) {
return value.match(/^([a-zA-Z0-9]+)$/);
});
$.validator.addClassRules({
rules:{
alpha_numeric: true,
},
message:{
alpha_numeric: 'should be alpha numeric only',
},
});