2014-10-29 85 views
1

我想添加一個額外的字段來驗證,當用戶點擊特定的div或文本。這裏是場景,我有一個使用jQuery驗證驗證的表單,有兩個字段我沒有包含驗證,爲了我希望當用戶點擊「更改密碼?」時驗證這兩個字段。文本。我怎麼能添加和額外的領域來驗證運行時?jQuery驗證,附加一個添加字段來驗證div點擊

<span class="change-password">Change Password</span> 

$(document).ready(function() { 
$("#update_user").validate({ 

     rules: { 
      "admin_account[name]": { 
      required: true 
      }, 
      "admin_account[email]": { 
      required: true 
      }, 
      "admin_account[gender]":{ 
      required: true 
      }, 
      "admin_account[nationality]":{ 
      required: true 
      }, 
      "admin_account[photo]":{ 
      accept: false 
      }, 
      "admin_account[date_of_birth(1i)]":{ 
      required: true 
      }, 
      "admin_account[date_of_birth(2i)]":{ 
      required: true 
      }, 
      "admin_account[date_of_birth(3i)]":{ 
      required: true 
      } 
     }, 
     errorPlacement: function(){ 
      return false; // suppresses error message text 
     } 

     }); 

//I want to trigger here an additional validation for the password field 
$(".change-password").on("click", function(){ 
    $("#update_user").validate({ 

     rules: { 
      "admin_account[name]": { 
      required: true 
      }, 
      "admin_account[email]": { 
      required: true 
      }, 
      "admin_account[password]":{ 
      required: true 
      }, 
      "admin_account[password_confirmation]":{ 
      required: true, 
      equalTo:"#edit_registerPassword" 
      }, 
      "admin_account[gender]":{ 
      required: true 
      }, 
      "admin_account[nationality]":{ 
      required: true 
      }, 
      "admin_account[photo]":{ 
      accept: false 
      }, 
      "admin_account[date_of_birth(1i)]":{ 
      required: true 
      }, 
      "admin_account[date_of_birth(2i)]":{ 
      required: true 
      }, 
      "admin_account[date_of_birth(3i)]":{ 
      required: true 
      } 
     }, 
     errorPlacement: function(){ 
      return false; // suppresses error message text 
     } 

     }); 
}); 
+0

當用戶點擊'.change-password'元素時會發生什麼? – undefined 2014-10-29 01:16:05

+0

它顯示兩個字段,用於密碼字段並確認密碼。我希望這兩個領域也將得到驗證。當用戶點擊「取消」時,這兩個字段將被隱藏起來,不再驗證 – user3122063 2014-10-29 01:19:10

回答

1

jQuery驗證有一個ignore屬性,它從驗證過程中排除指定的元素。默認情況下,它的值設置爲:hidden,它正是你想要的,即它排除隱藏的元素。只需在第一個validate呼叫中定義規則並刪除第二個validate函數。

+0

非常感謝@vohuman。正是我在找的 – user3122063 2014-10-29 01:29:34

+0

@ user3122063非常歡迎您! – undefined 2014-10-29 01:34:48

相關問題