jQuery Validate 外掛程式為表單提供了強大的驗證功能,讓用戶端表單驗證變得更簡單,同時提供了大量的定製選項,滿足應用程式各種需求。該外掛程式捆綁了一套有用的驗證方法,包括 URL 和電子郵件驗證,同時提供了一個用來編寫使用者自訂方法的 API。所有的捆綁方法預設使用英語作為錯誤資訊,且已翻譯成其他 37 種語言。
該外掛程式是由 Jörn Zaefferer 編寫和維護的,他是 jQuery 團隊的一名成員,是 jQuery UI 團隊的主要開發人員,是 QUnit 的維護人員。該外掛程式在 2006 年 jQuery 早期的時候就已經開始出現,並一直更新至今。目前版本是 1.14.0。
訪問 jQuery Validate 官網,下載最新版的 jQuery Validate 外掛程式。
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script><script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
預設校正規則
| 序號 |
規則 |
描述 |
| 1 |
required:true |
必須輸入的欄位。 |
| 2 |
remote:"check.php" |
使用 ajax 方法調用 check.php 驗證輸入值。 |
| 3 |
email:true |
必須輸入正確格式的電子郵件。 |
| 4 |
url:true |
必須輸入正確格式的網址。 |
| 5 |
date:true |
必須輸入正確格式的日期。日期校正 ie6 出錯,慎用。 |
| 6 |
dateISO:true |
必須輸入正確格式的日期(ISO),例如:2009-06-23,1998/01/22。只驗證格式,不驗證有效性。 |
| 7 |
number:true |
必須輸入合法的數字(負數,小數)。 |
| 8 |
digits:true |
必須輸入整數。 |
| 9 |
creditcard: |
必須輸入合法的信用卡號。 |
| 10 |
equalTo:"#field" |
輸入值必須和 #field 相同。 |
| 11 |
accept: |
輸入擁有合法尾碼名的字串(上傳檔案的尾碼)。 |
| 12 |
maxlength:5 |
輸入長度最多是 5 的字串(漢字算一個字元)。 |
| 13 |
minlength:10 |
輸入長度最小是 10 的字串(漢字算一個字元)。 |
| 14 |
rangelength:[5,10] |
輸入長度必須介於 5 和 10 之間的字串(漢字算一個字元)。 |
| 15 |
range:[5,10] |
輸入值必須介於 5 和 10 之間。 |
| 16 |
max:5 |
輸入值不能大於 5。 |
| 17 |
min:10 |
輸入值不能小於 10。 |
預設提示
messages: { required: "This field is required.", remote: "Please fix this field.", email: "Please enter a valid email address.", url: "Please enter a valid URL.", date: "Please enter a valid date.", dateISO: "Please enter a valid date ( ISO ).", number: "Please enter a valid number.", digits: "Please enter only digits.", creditcard: "Please enter a valid credit card number.", equalTo: "Please enter the same value again.", maxlength: $.validator.format( "Please enter no more than {0} characters." ), minlength: $.validator.format( "Please enter at least {0} characters." ), rangelength: $.validator.format( "Please enter a value between {0} and {1} characters long." ), range: $.validator.format( "Please enter a value between {0} and {1}." ), max: $.validator.format( "Please enter a value less than or equal to {0}." ), min: $.validator.format( "Please enter a value greater than or equal to {0}." )}
jQuery Validate提供了中文資訊提示包,位於下載包的 dist/localization/messages_zh.js,內容如下:
(function( factory ) { if ( typeof define === "function" && define.amd ) { define( ["jquery", "../jquery.validate"], factory ); } else { factory( jQuery ); }}(function( $ ) {/* * Translated default messages for the jQuery validation plugin. * Locale: ZH (Chinese, 中文 (Zhōngwén), 漢語, 漢語) */