簡體   English   中英

jQuery 驗證插件:條件文本字段驗證

[英]jQuery Validation plugin: Conditional textfield validation

我為這個問題困擾了三天。 我是一個前端開發人員,編程背景很少,所以我對 javascript 知之甚少。

問題是這樣的:我有兩個文本字段。 如果兩個文本字段都是 NULL,則填寫其中一個字段時會出現錯誤驗證。 如果其中一個文本字段已填寫,則另一個將不再是必填字段。

這是我為 HTML 准備的:

<ul>
 <li>
     <label for="billingAcctNo">Your account number:&nbsp;*</label>
             <input type="text" name="billingAcctNo" id="billingAcctNo" class="textfield" />
       </li>
       <li>
             <label for="billingMobileNo">Or your mobile phone number:&nbsp;*</label>
     <input type="text" name="billingMobileNo" id="billingMobileNo" class="textfield" />
       </li>

這就是我的驗證:

$("#form").validate({
      rules: {
          billingAcctNo:    {
           required: "#serviceBilling:checked"
      },
      billingMobileNo: {
            required: "#serviceBilling:checked",
            phoneUS: true
        }
       },
    messages: {
                 billingAcctNo: "Please enter a valid account number or mobile number",
        billingMobileNo: {
            required: "Please enter a valid account number or mobile number",
            phoneUS: "Please enter a valid mobile phone number"
        }
});

非常感謝! 我非常感謝您的反饋!

將您的規則配置更改為如下所述。

rules: {
          billingAcctNo:    {
           required: true
      },
      billingMobileNo: {
            required: true,
            phoneUS: true
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM