2016-09-28 19 views
-1
$.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\.))?[[email protected]:%._\+~#=]{2,256}\.[a-z]{2,4}\b([[email protected]:%_\+.~#?&//=]*)" 
    }, 
    }); 

我認爲正則表達式會破壞字符串頂點,所以它不起作用。我怎麼解決這個問題?在jQuery驗證程序中使用正則表達式

+0

使用正則表達式文字語法。用'/'替換括起來的引號。 – Tushar

回答

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