jQuery外掛程式 -- 表單驗證外掛程式jquery.validate.js

來源:互聯網
上載者:User

標籤:信用卡   eth   copy   即時   data-   div   cli   支援   word   

最常使用JavaScript的場合就是表單的驗證,而jQuery作為一個優秀的javascript庫,也提供了一個優秀的表單驗證外掛程式----Validation。Validation是曆史最悠久的jquery外掛程式之一,經過了全球範圍內不同項目的驗證,並得到了許多Web開發人員的好評。作為一個標準的驗證方法庫,Validation擁有如下特點:

1.內建驗證規則: 擁有必填、數字、Email、URL和信用卡號碼等19類內建驗證規則

2.自訂驗證規則: 可以很方便地自訂驗證規則

3.簡單強大的驗證資訊提示: 預設了驗證資訊提示,並提供自訂覆蓋預設的提示資訊的功能

4.即時驗證: 可能通過keyup或blur事件觸發驗證,而不僅僅在表單提交的時候驗證

validate.js: http://plugins.jquery.com/project/validate

metadata.js: http://plugins.jquery.com/project/metadata

使用方法:
1.引入jQuery庫和Validation外掛程式

 

[javascript] view plain copy  print?
  1. <script src="scripts/jquery-1.6.4.js" type="text/javascript"></script>  
  2. <script src="scripts/jquery.validate.js" type="text/javascript"></script>  

 

2.確定哪個表單需要被驗證

 

[javascript] view plain copy  print?
  1. <script type="text/javascript">  
  2. ////<![CDATA[  
  3. $(document).ready(function(){  
  4.     $("#commentForm").validate();  
  5. });  
  6. //]]>  
  7. </script>  

3.針對不同的欄位,進行驗證規則編碼,設定欄位相應的屬性

 

[javascript] view plain copy  print?
  1. class="required"   必須填寫  
  2. class="required email"        必須填寫且內容符合Email格式驗證  
  3. class="url"             符合URL格式驗證  
  4. minlength="2"      最小長度為2  

可驗證的規則有19種:

 

[javascript] view plain copy  print?
  1. required:      必選欄位  
  2. remote:        "請修正該欄位",  
  3. email:         電子郵件驗證  
  4. url:           網址驗證  
  5. date:          日期驗證  
  6. dateISO:       日期 (ISO)驗證  
  7. dateDE:  
  8. number:        數字驗證  
  9. numberDE:  
  10. digits:        只能輸入整數  
  11. creditcard:    信用卡號驗證  
  12. equalTo:       ”請再次輸入相同的值“驗證  
  13. accept:        擁有合法尾碼名的字串驗證  
  14. maxlength/minlength:    最大/最小長度驗證  
  15. rangelength:     字串長度範圍驗證  
  16. range:           數字範圍驗證  
  17. max/min:         最大值/最小值驗證  

另個一種驗證方法(將所有的與驗證相關的資訊寫到class屬性中方便管理)

1.引入一個新的jQuery外掛程式---jquery.metadata.js(支援固定格式解析的jQuery外掛程式)

 

[javascript] view plain copy  print?
  1. <script src="scripts/jquery.metadata.js" type="text/javascript"></script>  

2.改變調用的驗證方法

 

[javascript] view plain copy  print?
  1. <script type="text/javascript">  
  2. ////<![CDATA[  
  3. $(document).ready(function(){  
  4.     //將$("#commentForm").validate();  改成  
  5.     $("#commentForm").validate({meta: "validate"});  
  6. });  
  7. //]]>  
  8. </script>  

3.將驗證規則全部編寫到class屬性中

 

[javascript] view plain copy  print?
  1. class="{validate:{required: true, minlength: 2, messages:{required:‘請輸入姓名‘, minlength:‘請至少輸入兩個字元‘}}}"  
  2. class="{validate:{required: true, email: true, messages:{required:‘請輸入電子郵件‘, email:‘請檢查電子郵件的格式‘}}}"       

也可以通過name屬性來關聯欄位和驗證規則的驗證寫法(驗證行為和HTML結構完全脫鉤)

 

[javascript] view plain copy  print?
  1. $("#commentForm").validate({  
  2.    rules: {  
  3.       username: {  
  4.          required: true,  
  5.          minlength: 2  
  6.       },  
  7.       email: {  
  8.          required: true,  
  9.          email: true  
  10.       },  
  11.       url:"url",  
  12.       comment: "required"  
  13.    },  
  14.    messages: {  
  15.       username: {  
  16.          required: ‘請輸入姓名‘,  
  17.          minlength: ‘請至少輸入兩個字元‘  
  18.       },  
  19.       email: {  
  20.          required: ‘請輸入電子郵件‘,  
  21.          email: ‘請檢查電子郵件的格式‘  
  22.       },  
  23.       url: ‘請檢查網址的格式‘,  
  24.       comment: ‘請輸入您的評論‘  
  25.    }  
  26. });  

國際化

Validation外掛程式的驗證資訊預設語言是英文,如果要改成中文,只需要引入Validation提供的中文驗證資訊即可,引入代碼如下:

 

[javascript] view plain copy  print?
  1. <script src="scripts/jquery.validate.messages_cn.js" type="text/javascript"></script>  

自訂驗證資訊並美化

 

 

[javascript] view plain copy  print?
  1. errorElement: "em",               //可以用其他標籤,記住把樣式也對應修改  
  2. success: function(label) {    //label指向上面那個錯誤提示資訊標籤em  
  3.       label.text("")                       //清空錯誤提示訊息  
  4.               .addClass("success");    //加上自訂的success類  
  5.      }  
  6. 在CSS中添加樣式與之關聯  
  7. em.error {  
  8.       background:url("images/unchecked.gif") no-repeat 0px 0px;  
  9.       padding-left: 16px;  
  10. }  
  11. em.success {  
  12.       background:url("images/checked.gif") no-repeat 0px 0px;  
  13.       padding-left: 16px;  
  14. }  

自訂驗證規則

 

[javascript] view plain copy  print?
    1. //自訂一個驗證方法  
    2. $.validator.addMethod(  
    3.     "formula", //驗證方法名稱  
    4.     function(value, element, param) {//驗證規則  
    5.         return value == eval(param);  
    6.     },   
    7.     ‘請正確輸入數學公式計算後的結果‘//驗證提示資訊  
    8. );  
    9.   
    10. $("#commentForm").validate({  
    11.    rules: {  
    12.       username: {  
    13.          required: true,  
    14.          minlength: 2  
    15.       },  
    16.       email: {  
    17.          required: true,  
    18.          email: true  
    19.       },  
    20.       url:"url",  
    21.       comment: "required",  
    22.       valcode: {  
    23.          formula: "7+9"  
    24.       }  
    25.    },  
    26.    messages: {  
    27.       username: {  
    28.          required: ‘請輸入姓名‘,  
    29.          minlength: ‘請至少輸入兩個字元‘  
    30.       },  
    31.       email: {  
    32.          required: ‘請輸入電子郵件‘,  
    33.          email: ‘請檢查電子郵件的格式‘  
    34.       },  
    35.       url: ‘請檢查網址的格式‘,  
    36.       comment: ‘請輸入您的評論‘,  
    37.       valcode: ‘驗證碼錯誤‘  
    38.    }  
    39. });  

jQuery外掛程式 -- 表單驗證外掛程式jquery.validate.js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.