验证插件 - 使用submitHandler

时间:2009-07-18 11:05:44

标签: javascript jquery validation

我很抱歉,如果您已经看过这个或回复过它,但我似乎无法找到我最初提交的问题,而且我迫切希望找到解决方案。

我需要在用户点击提交时显示促销消息 他们满足关于邮政编码的某些标准。我听说过你 应该使用submitHandler,但对JavaScript来说是全新的 甚至更新的JQuery我有点困惑我应该怎么写 这个。我觉得它会是这样的,但显然 不起作用。

submitHandler: function(form) { 
                    var special = /^[T]{1}[A]{1}([1-13|17|25]){2}/.test(value); 
                    if (special); 
                    alert('You have been entered into a competition to win a special    prize'); 
                    form.submit(); 
            }, // end of submitHandler 

我基本上需要在他们点击提交时显示此消息 然后他们确认并将表格发送到服务器。

这是我到目前为止验证的代码:

 $(document).ready(function(){ 
 $("#orderForm").validate({ 
            onfocusout: function(element) { 
                    this.element(element); 
            }, 
            rules: { 
                    firstName: { 
                            required: true, 
                    }, 
                    surname: { 
                            required: true, 
                    }, 
                    phoneNumber: { 
                            required: true, 
                    }, 
                    streetName: { 
                            required: true, 
                    }, 
                    city: { 
                            required: true, 
                    }, 
                    postalCode: { 
                            required: true, 
                            shipPostalCode: true, 
                    }, 
                    billEmailAddress: { 
                            required: true, 
                    }, 
                    billPhoneNumber: { 
                            required: true, 
                    }, 
                    promoCardNumber: { 
                            required: true, 
                            fidelityCardNumber: true, 
                    }, 
                    billCardNumber: { 
                            required: true, 
                    }, 
                    billCardType: { 
                            required: true, 
                    }, 
            }, //end of rules 
    }); // end of validate 
    }); // end of function 


$.validator.addMethod('postalCode', function (value) { 
            return /^[A-Z]{2}\d{1,2}\s\d{1}[A-Z]{2}$/.test(value); 
            }, 'Please enter a valid postcode'); 
$.validator.addMethod('promoCardNumber', function (value) { 
            return /^[A-Z]{1}([A-Z]|\d){4}\s?([A-Z]|\d){5}\s?([A-Z]|\d){3}\d{1}$/.test  (value); 
            }, 'Please enter a valid card number'); 

这是我的HTML代码:

   <form id="orderForm" method="post" action="x"> 
  <table id="formTable" cellpadding="5"> 
    <tr> 
      <td> 
        <h3>Shipping and Billing Information</h3> 
      </td> 
      <td>&nbsp;</td> 
    </tr> 
    <tr> 
      <td><label for="firstname">First Name</label></td> 
      <td><input id="firstName" type="text" name="firstName" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="surname">Surname</label></td> 
      <td><input id="surname" type="text" name="surname" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="phoneNumber">Contact Telephone Number</label></td> 
      <td><input id="phoneNumber" type="text" name="phoneNumber" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="streetName">Street Name</label></td> 
      <td><input id="streetName" type="text" name="streetName" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="city">City</label></td> 
      <td><input id="city" type="text" name="city" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="postalCode">Post Code</label></td> 
      <td><input id="postalCode" type="text" name="postalCode" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="billEmailAddress">Email address</label></td> 
      <td><input id="billEmailAddress" type="text" name="billEmailAddress" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="billPhoneNumber">Contact Telephone Number</label></td> 
      <td><input id="billPhoneNumber" type="text" name="billPhoneNumber" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="promoCardNumber">Promotional Card</label></td> 
      <td><input id="promoCardNumber" type="text" name="promoCardNumber" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="billCardNumber">Credit Card Number</label></td> 
      <td><input id="billCardNumber" type="text" name="billCardNumber" maxlength="30" /></td> 
    </tr> 
    <tr> 
      <td><label for="billCardType">Credit Card Type</label></td> 
      <td><select id="billCardType" name="billCardType"> 
        <option value="..."> 
          Choose your card... 
        </option> 
        <option value="visa"> 
          Visa 
        </option> 
        <option value="mastercard"> 
          Mastercard 
        </option> 
      </select></td> 
    </tr> 
    <tr> 
      <td><label for="instructions">Instructions</label></td> 
      <td> 
      <textarea id="instructions" name="instructions" rows="8" cols="30"> 
Enter your requirements here or comments. </textarea></td> 
    </tr> 
    <tr> 
      <td colspan="2"><input id="submit" type="submit" name="Submit" value="Submit" /> 
              </td> 
    </tr> 
  </table> 
</form> 

1 个答案:

答案 0 :(得分:1)

我认为您的原始问题(以及我对答案的尝试)在这里:JQuery and validate plugin - how to add a promotional message on submit