jquery.validate.js簡介

來源:互聯網
上載者:User

參看:http://docs.jquery.com/Plugins/Validation並整理

 

jquery.validate.js是jquery旗下的一個驗證架構,藉助jquery的優勢,我們可以迅速驗證一些常見的輸入,並且可以自己擴充自己的驗證方法,並且對國際化也有很好的支援

 

使用這個函數很簡單,看以下的代碼

Html代碼
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
      
  2.                     "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5.   <script src="http://code.jquery.com/jquery-latest.js"></script>  
  6.   <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.css" type="text/css" media="screen" />  
  7. <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>  
  8. <style type="text/css">  
  9. * { font-family: Verdana; font-size: 96%; }   
  10. label { width: 10em; float: left; }   
  11. label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
      
  12. p { clear: both; }   
  13. .submit { margin-left: 12em; }   
  14. em { font-weight: bold; padding-right: 1em; vertical-align: top; }
      
  15. </style>  
  16.   <script>  
  17.   $(document).ready(function(){   
  18.     $("#commentForm").validate();   
  19.   });   
  20.   </script>  
  21.      
  22. </head>  
  23. <body>  
  24.      
  25.   
  26.  <form class="cmxform" id="commentForm" method="get" action="">  
  27.  <fieldset>  
  28.    <legend>A simple comment form with submit validation and default messages</legend>  
  29.    <p>  
  30.      <label for="cname">Name</label>  
  31.      <em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />  
  32.    </p>  
  33.    <p>  
  34.      <label for="cemail">E-Mail</label>  
  35.      <em>*</em><input id="cemail" name="email" size="25"  class="required email" />  
  36.    </p>  
  37.    <p>  
  38.      <label for="curl">URL</label>  
  39.      <em>  </em><input id="curl" name="url" size="25"  class="url" value="" />  
  40.    </p>  
  41.    <p>  
  42.      <label for="ccomment">Your comment</label>  
  43.      <em>*</em><textarea id="ccomment" name="comment" cols="22"  class="required"></textarea>  
  44.    </p>  
  45.    <p>  
  46.      <input class="submit" type="submit" value="Submit"/>  
  47.    </p>  
  48.  </fieldset>  
  49.  </form>  
  50. </body>  
  51. </html>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"                     "http://www.w3.org/TR/html4/loose.dtd"><html><head>  <script src="http://code.jquery.com/jquery-latest.js"></script>  <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.css" type="text/css" media="screen" /><script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script><style type="text/css">* { font-family: Verdana; font-size: 96%; }label { width: 10em; float: left; }label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }p { clear: both; }.submit { margin-left: 12em; }em { font-weight: bold; padding-right: 1em; vertical-align: top; }</style>  <script>  $(document).ready(function(){    $("#commentForm").validate();  });  </script>  </head><body>   <form class="cmxform" id="commentForm" method="get" action=""> <fieldset>   <legend>A simple comment form with submit validation and default messages</legend>   <p>     <label for="cname">Name</label>     <em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />   </p>   <p>     <label for="cemail">E-Mail</label>     <em>*</em><input id="cemail" name="email" size="25"  class="required email" />   </p>   <p>     <label for="curl">URL</label>     <em>  </em><input id="curl" name="url" size="25"  class="url" value="" />   </p>   <p>     <label for="ccomment">Your comment</label>     <em>*</em><textarea id="ccomment" name="comment" cols="22"  class="required"></textarea>   </p>   <p>     <input class="submit" type="submit" value="Submit"/>   </p> </fieldset> </form></body></html>

  總結,我們只要在加入如下的JAVASCRIPT代碼,就可以把指定的FORM加上驗證

Html代碼
  1. $(document).ready(function(){   
  2.    $("#commentForm").validate();   
  3.  });  
 $(document).ready(function(){    $("#commentForm").validate();  });

 
不過我們還要在需要驗證的INPUT裡面class加入required說明是必填項,其他的就是驗證相關資料比如email就是驗證email的資料結構

以下列出validate內建的預設驗證

 

        required: "必選欄位",
        remote: "請修正該欄位",
        email:
"請輸入正確格式的電子郵件",
        url: "請輸入合法的網址",
        date: "請輸入合法的日期",
   
    dateISO: "請輸入合法的日期 (ISO).",
        number: "請輸入合法的數字",
       
digits: "只能輸入整數",
        creditcard: "請輸入合法的信用卡號",
        equalTo:
"請再次輸入相同的值",
        accept: "請輸入擁有合法尾碼名的字串",
        maxlength:
jQuery.format("請輸入一個長度最多是 {0} 的字串"),
        minlength:
jQuery.format("請輸入一個長度最少是 {0} 的字串"),
        rangelength:
jQuery.format("請輸入一個長度介於 {0} 和 {1} 之間的字串"),
        range:
jQuery.format("請輸入一個介於 {0} 和 {1} 之間的值"),
        max: jQuery.format("請輸入一個最大為
{0} 的值"),
        min: jQuery.format("請輸入一個最小為 {0} 的值")

 

validate ()的可選項debug:進行偵錯模式
$(".selector").validate



({
debug: true
})

  把調試設定為預設

 

$.validator.setDefaults



({
debug: true
})

 

submitHandler:用其他方式替代預設的SUBMIT,比如用AJAX的方式提交

 

$(".selector").validate



({
submitHandler: function(form) {
$(form).ajaxSubmit();
}
})

 

ignore:忽略某些元素不驗證

$("#myform").validate



({
ignore: ".ignore"
})

 

rules: 預設下根據form的classes, attributes,
metadata判斷,但也可以在validate函數裡面聲明
Key/value 可自訂規則. Key是對象的名字 value是對象的規則,可以組合使用
class/attribute/metadata rules.

以下代碼特別驗證selector類中name='name'是必填項並且 email是既是必填又要符合email的格式

 

$(".selector").validate

({
rules: {
// simple rule, converted to {required:true}
name: "required",
// compound rule
email: {
required: true,
email: true
}
}
})

 

messages:更改預設的提示資訊

 

$(".selector").validate

({
rules: {
name: "required",
email: {
required: true,
email: true
}
},
messages: {
name: "Please specify your name",
email: {
required: "We need your email address to contact you",
email: "Your email address must be in the format of name@domain.com"
}
}
})

groups:定義一個組,把幾個地方的出錯資訊同意放在一個地方,用errorPlacement控制把出錯資訊放在哪裡

 

$("#myform").validate
({
groups: {
username: "fname lname"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "fname" || element.attr("name") == "lname" )
error.insertAfter("#lastname");
else
error.insertAfter(element);
},
debug:true
})
  • jquery.validate.zip
    (211.8 KB)
  • 下載次數: 23

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.