2012-10-15 163 views
3

我遇到了我的jQuery验证脚本的问题。jQuery验证不验证

你可以找到http://jsfiddle.net/R3wrn/1/

的jQuery代码:

$(document).ready(function() { 
    $("#frmContact").validate({ 
     rules: { 
      contact_name: "required", 
      contact_firstname: "required", 
      contact_email: { 
       required: true, 
       email: true 
      }, 
      contact_company: "required", 
      contact_VAT: "required", 
      contact_Address: "required", 
      contact_ZIP: "required", 
      contact_city: "required", 
      contact_country: "required" 
     } 
    }); 
}); 

HTML:

<form id="frmContact" action="#" method="post" class="f2m contactForm"> 
    <div class="inputgroup"> 
     <label for="contact_name">Name</label> 
     <input type="text" id="contact_name" value="" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_firstname">First name</label> 
     <input type="text" id="contact_firstname" value="" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_email">Email</label> 
     <input type="text" id="contact_email" value="" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_name">Company</label> 
     <input type="text" id="contact_company" value="" class="required" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_VAT">VAT number</label> 
     <input type="text" id="contact_VAT" value="" class="required" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_Address">Address</label> 
     <input type="text" id="contact_Address" value="" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_ZIP">Postal code</label> 
     <input type="text" id="contact_ZIP" value="" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_city">City</label> 
     <input type="text" id="contact_city" value="" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_country">Country</label> 
     <select id="contact_country"> 
      <option value="">---</option> 
      <option value="8b902045-d885-4777-86fb-e44788d842e3">Belgi&#235;</option> 
      <option value="1a839cad-d0a0-449e-b376-96f120e78c9e">Duitsland</option> 
      <option value="bcabef32-78eb-4489-a90a-cfa42e525053">France</option> 
      <option value="788806bb-c3ec-4a42-a651-64e676065b7d">Griekenland</option> 
      <option value="542063ac-025b-44c5-8324-f36bde1c7989">Groot-Brittanni&#235;</option> 
      <option value="c5159342-0cbb-4f9d-b449-71d16e0953e0">Itali&#235;</option> 
      <option value="65c9621d-4f6a-45d4-9d8c-25d6e27432d3">Luxemburg</option> 
      <option value="14874203-02c2-4999-a0a5-aabcacf21530">Nederland</option> 
      <option value="135523df-5161-4c90-865c-cbf280569631">Noorwegen</option> 
      <option value="e0954d61-d0f3-498a-850f-33a54be040d5">Portugal</option> 
      <option value="ab0a9f35-206e-419b-9fb9-b8bbd51066cb">Spanje</option> 
      <option value="19766c79-51be-4094-95e5-6a981a452491">Zweden</option> 
     </select> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_mobile">Mobile</label> 
     <input type="text" id="contact_mobile" value="" /> 
    </div> 
    <div class="inputgroup"> 
     <label for="contact_comments">Comments</label> 
     <textarea id="contact_comments" rows="10" cols="10"></textarea> 
    </div> 
    <div class="buttondiv"> 
     <br /> 
     <input type="submit" id="btnSendContact" class="button" value="Send your request" /> 
    </div> 
</form> 

为什么仅在验证companyname

我在ASP.NET MVC应用程序中或多或少有相同的代码,但它不会验证任何内容。

回答

3

jQuery验证插件使用name属性,而不是id。尝试将输入的id属性更改为name

+0

作品!侧面问题:我如何防止“此字段是必需的”。字符串显示? – JurgenStillaert

+0

您可以像这样设置消息选项: 消息:{contact_name:“”,contact_firstname:“”,..}。但我想有更好的方法来做到这一点.. –

+0

http://www.meppum.com/2009/may/12/disabling-jquery-validation-error-messages/ – JurgenStillaert