jquery-validation外掛程式

來源:互聯網
上載者:User

標籤:round   sub   調試   表單驗證   標記   參數   選擇   form   and   

jQuery Validation外掛程式為表單提供了強大的驗證功能,讓用戶端表單驗證變得更簡單,同時提供了大量的定製選項,滿足應用程式各種需求。

一、下載

外掛程式:https://jqueryvalidation.org/

二、使用

引入js檔案:

<script src="jquery-1.11.3.min.js"></script><script src="jquery.validate-1.13.1.js"></script>

js代碼:

$("form").validate(    //配置項);

三、API

1、rules:定義校正規則

rules:{            first_word_input:{                required:true            },            brand_choose_input:{                required:true            }        }

2、messages:定義提示資訊

messages:{            first_word_input:{                required:"*請選擇首字母"            },            brand_choose_input:{                required:"*請選擇品類"            }        }

3、ignore:對某些元素不進行驗證

4、debug:只驗證不提交

如果這個參數為true,那麼表單不會提交,只進行檢查,調試時十分方便。

 debug:true

5、errorClass:指定錯誤提示的 css 類名,可以自訂錯誤提示的樣式

6、errorElement: 使用什麼標籤標記錯誤

7、errorPlacement:Callback 指明錯誤放置的位置,預設情況是:error.appendTo(element.parent());即把錯誤資訊放在驗證的元素後面。

errorPlacement : function(error, element) {            //忽略自訂的方法錯誤提示            if (error.text() == "ignore") {                return true;            }            error.appendTo(element.parents(".form-group"));        }

8、submitHandler: 通過驗證後啟動並執行函數,可以加上表單提交的方法

 submitHandler:function(form){        form.submit();         //form是DOM對象}

9、驗證的觸發方式修改

10、自訂驗證方法

 jQuery.validator.addMethod(name,method,[message]);

參數:

  name:方法名稱  method:function(value,element,params)方法邏輯,value是元素的值,element是元素本身 param是參數  message:提示訊息
// 郵遞區號驗證   jQuery.validator.addMethod("isZipCode", function(value, element) {       var tel = /^[0-9]{6}$/;    return this.optional(element) || (tel.test(value));}, "請正確填寫您的郵遞區號");

 

四、執行個體

$("form").validate({    debug:true,    rules:{        first_word_input:{                required:true            },            brand_choose_input:{                required:true            }    },    messages:{            first_word_input:{                required:"*請選擇首字母"            },            brand_choose_input:{                required:"*請選擇品類"            }    },    errorClass:"error_tip",    errorElement: "label",    errorPlacement : function(error, element) {            //忽略自訂的方法錯誤提示        if (error.text() == "ignore") {                return true;            }        error.appendTo(element.parents(".form-group"));    },    submitHandler:function(form){            form.aubmit();    }              });
jQuery.validator.addMethod(
"isZipCode", function(value, element) { var tel = /^[0-9]{6}$/; return this.optional(element) || (tel.test(value));}, "請正確填寫您的郵遞區號");

 

jquery-validation外掛程式

聯繫我們

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