2010-03-08 38 views
0

我正在使用jQuery的驗證插件: http://docs.jquery.com/Plugins/Validation如何免除要驗證的元素?

我想知道如何豁免一個字段進行驗證?目前表單中的所有字段都會自動檢查。


我不知道這是否會因爲我想免除不包括(顯然:))元素有助於

$("#contactForm").validate({ 
     rules: { 
         first_name:{required: true,charsonly:true,minlength: 1 }, 
         last_name:{required:true,charsonly:true,minlength: 1 }, 
         birthday:{required:true,min:1,max:31 }, 
         birthmonth:"required", 
         birthyear:"required", 
         username: {required: true,minlength:1,maxlength:32,username:true,notChinese:true,noSpecial:true}, 
         password1:{required:true,minlength: 5,maxlength: 10, alphaNum: true }, 
         password2:{required:true,minlength: 5,maxlength: 10, alphaNum: true, equalTo: "#password1"}, 
         currency:"required", 
         address:{required: true,noSpecial:true,minlength:2}, 
         city:{required: true,noSpecial:true,minlength:2}, 
         state:{noSpecial:true}, 
         countrycode:"required", 
         zip:{required:true,zipTest:true}, 
         email:{required:true,email: true}, 
         confirmemail:{required: true,equalTo: "#email",email:true}, 
         phone:{required: true,minlength:6,maxlength:20,phoneUS:true}, 
         cellphone:{required: true,minlength:6,maxlength:20,phoneUS:true}, 
         custom06:{acceptIM:true} 

     } }); 

發現了它。 我使用了忽略驗證方法。


這裏的

$("#myform").validate({ 
    ignore: ".ignore" 
}) 
+0

只是爲了澄清任何信息領域。我想要做的就是刪除這個插件,即使沒有對該元素進行驗證,該插件也會自動附加這段代碼。 – wnoveno 2010-03-08 07:02:14

+0

看來問題源於使用全局規則函數而不是設置每個元素的類。我仍然不知道如何解決這個問題。 – wnoveno 2010-03-08 07:55:00

回答

0

您正在使用某種形式的服務器端技術的代碼?驗證庫需要設置規則,如果您嚴格按照javaScript(jQuery)進行操作,那麼它是手動的。

UPDATE:您可以刪除規則有以下:http://docs.jquery.com/Plugins/Validation/rules#.22remove.22rules

發生在身體的末端以下。

$("selector").rules("remove"); 
+0

不。並且我想豁免的領域不在規則中(這意味着我沒有添加任何內容,因爲我不想驗證它)。 – wnoveno 2010-03-08 06:41:39

+0

請粘貼你的源代碼。 – 2010-03-08 06:55:01

0

它取決於類屬性。見例子:

此爲必填字段和電子郵件的格式:它不能爲空

input class="required email" 

此字段不是必需的,電子郵件的格式:它可以爲空

input class="email" 

此字段不需要和沒有格式:它可以是空的

input class="" 
+0

這不起作用,因爲我已將圖像添加到插件附加到每個元素(x或檢查)的標籤上。 – wnoveno 2010-03-08 07:01:22

+0

讓我們看看完整的代碼。 – Sergei 2010-03-08 07:52:50

0

您可以設置自己的消息:

$("#contactForm").validate({ 
     rules: { 
     input1: required, 
     input2: required, 
     no-validation: required, 
     input4: required, 
     }, 
     messages: { 
     no-validation: '' 
     } 
}); 

只要把一個空字符串爲您不想將顯示..