2016-11-24 37 views
0

我試圖使用jQuery驗證插件來實時驗證一些文本字段。其中一個規則是字段不能包含'http'。我如何修改validate()方法來實現這一點?jquery驗證文本不能包含

@using (Html.BeginForm("UserDetails", "Account", FormMethod.Post, new {id = "registerform" })) 
{ 
    <input id="Facebook" minlength="2" name="Facebook" placeholder="eg: Joe.Bloggs123" type="text" value> 
    ... 
} 

<script> 
    $('#registerform').validate({ 
     // ...? 
    }) 
</script> 

編輯:我試着添加一個validator.addmethod,但它仍然不起作用。因此,這裏是我的腳本:

$('#registerform').validate(); 

jQuery.validator.addMethod("Facebook", function(value, element) { 
      return this.optional(element) || (value.indexOf('http') >= 0); 
     }, "*Please just use your userid, not the full url"); 

此外,我需要驗證大量的這些,是不是可以添加自定義類,並從該選擇?

+1

u可以使用自定義的驗證方法,這 –

+0

閱讀文檔:https://jqueryvalidation.org/ jQuery.validator.addMethod /並告訴我們你試過的 –

+0

編輯的OP與我試着的方法 – Jynn

回答

3

這裏是終於爲我工作:

<script> 
    jQuery.validator.addMethod("checkhttp", function(value, element) { 
         return this.optional(element) || (value.indexOf('http') < 0) && (value.indexOf('www') < 0); 
        }, "*Please just use your userid, not the full url"); 

        jQuery.validator.classRuleSettings.checkhttp = { checkhttp: true }; 

       $('#registerform').validate(); 
</script> 

@using (Html.BeginForm("UserDetails", "Account", FormMethod.Post, new {id = "registerform" })) 
{ 
    <input id="Facebook" class="checkhttp" name="Facebook" placeholder="eg: Joe.Bloggs123" type="text" value> 
} 
-1

這是你的情況,但我建議你使用正則表達式

編輯 我很抱歉,我沒有看到這個問題的權利。

添加一個jQuery驗證

jQuery.validator.addMethod("DoesNotContain", function(value, element) 
{ 
    return this.optional(element) || (indexOf('http') < 0); 
}, "* Can't contain http"); 
+0

這不是一個jquery.validation實現 –