在jquery验证器中使用正则表达式

时间:2023-01-01 20:12:47
$.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@:%_\+.~#?&//=]*)"
     },
   });

I think that the regular expression breaks the string apexes so it doesn't work. how can i solve this problem?

我认为正则表达式打破了字符串顶点,所以它不起作用。我怎么解决这个问题?

1 个解决方案

#1


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',
      },
});

#1


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',
      },
});