2010-10-07 62 views
0

我正在使用Jquery valdiation plugin驗證客戶端的表單。但我想要進行驗證。基於屬性的使用jquery插件的自定義表單驗證

<input type="text" id="txtInf" regex="/some regular expression/" error="Inf is mandatory"></inf> 

這裏正則表達式錯誤是自定義屬性。字段將針對正則表達式中的給定正則表達式進行驗證,如果正則表達式文本失敗,則應顯示錯誤消息。

我試圖添加一個方法到驗證器像這樣。

$("*[regex]").each(function() { 

      $.validator.addMethod($(this).attr('id'), function() { 
       return this.optional($(this)) || $(this).attr('regex').test($(this).text()); 
      }, $(this).attr('error')); 

}); 

但是這種方法存在一些問題。請讓我知道,如果我認爲是正確的。 如果您還有其他方法,請告訴我。歡迎任何思考過程。

回答

0

我沒有使用該插件,但它看起來像使用test()會出錯。

$(this).attr('regex').test($(this).text()); 

應該

var regEx = new RegExp($(this).attr('regex')); 
regEx.test($(this).text()); 
相關問題