jQuery驗證框架
五、驗證器( Validator )
validate方法返回的驗證器對象( Validator Object) 有一些公用的方法。你可以用來觸發驗證程序或改變表單(form)的內容。驗證器對象有更多的方法,不過只有文檔中給出的這些方法是專爲使用而設計的。
(一)驗證器方法(Validator methods)
[1] form() 返回:Boolean
說明:驗證表單是否通過驗證,若通過驗證則返回true,反之返回false。這個方法在正常的提交事件(submit event)觸發,它返回一個結果。
- $("#myform").validate().form();
$("#myform").validate().form();
[2] element( element ) 返回:Boolean
參數 element 類型:Selector 驗證表單中的一個需要驗證的表單元素。
說明:驗證單個表單元素是否通過驗證,若通過驗證則返回true,反之返回false。這個方法在正常的焦點離開事件(blur)或按鍵彈起(keyup)時觸發,它返回一個結果。
- $("#myform").validate().element( "#myselect" );
$("#myform").validate().element( "#myselect" );
[3] resetForm( ) 返回:undefined
說明:重置表單。
恢復表單元素到原來的值(需要form插件支持),移除無效驗證的樣式並隱藏錯誤消息。
(...貌似只有在IE下纔可以移除樣式)
- var validator = $("#myform").validate();
- validator.resetForm();
var validator = $("#myform").validate(); validator.resetForm();
[4] showErrors( errors ) 返回:undefined
參數 errors 類型:Object<String, String> 一個或多個表單元素的name屬性和驗證消息組成的鍵/值對。
說明:顯示指定的驗證消息。
在指定的errorPlacement中顯示驗證消息。鍵爲待驗證表單元素的name屬性,值爲相應的驗證消息。
- var validator = $("#myform").validate();
- validator.showErrors({"firstname": "I know that your firstname is Pete, Pete!"});
var validator = $("#myform").validate(); validator.showErrors({"firstname": "I know that your firstname is Pete, Pete!"});
[5] numberOfInvalids( ) 返回:Integer
說明:返回未通過驗證的表單元素的個數。
這個方法依賴於內部的驗證器情況。只有在驗證完所有表單元素時才統計所有待驗證的表單元素(submit事件或通過$("form").valid())。當只驗證單個表單元素,則只有統計該表單元素。與invalidHandler選項聯合使用的時候非常有用。
- var validator = $("#myform").validate({
- invalidHandler: function() {
- $("#summary").text(validator.numberOfInvalids() +"field(s) areinvalid");
- }
- });
var validator = $("#myform").validate({ invalidHandler: function() { $("#summary").text(validator.numberOfInvalids() +"field(s) areinvalid"); } });
(二)驗證器函數(Validator functions)
[1] setDefaults( defaults ) 返回:undefined
參數 defaults 類型:Options 要設置成默認值的選項。
說明:修改驗證框架的默認設置。
接受validate方法中的所有選項。
- jQuery.validator.setDefaults({
- debug: true
- });
jQuery.validator.setDefaults({ debug: true });
[2] addMethod( name, method, [message] ) 返回:undefined
參數 name 類型:String 要添加的方法名,用於標識和引用,必須是一個有效的javascript標識符。
參數 method 類型:Callback 方法的實現部分,返回true如果表單元素通過驗證。
參數 message(Optional) 類型:String, Function 該方法的默認驗證消息。可以用 jQuery.validator.format(value) 方法創建。如果未定義該參數,則使用本地已存在的驗證消息,另外,必須爲指定的表單元素定義驗證消息。
說明:添加一個用戶自定義的驗證方法。它由方法名(必須是一個合法的javascript標識符)、基於javascript的函數及默認的驗證消息組成。
- jQuery.validator.addMethod("math", function(value, element, params) {
- return this.optional(element) || value == params[0] + params[1];
- }, jQuery.format("Please enter the correct value for {0} + {1}"));
jQuery.validator.addMethod("math", function(value, element, params) { return this.optional(element) || value == params[0] + params[1]; }, jQuery.format("Please enter the correct value for {0} + {1}"));
[3] addClassRules( name, rules ) 返回:undefined
參數 name 類型:String 要添加的樣式規則名。
參數 rules 類型:Options 規則選項。
說明:添加一個複合的樣式驗證方法。對於將多個聯合使用的規則重構進單個樣式中非常有用。
- jQuery.validator.addClassRules("name", {
- required: true,
- minlength: 2
- });
jQuery.validator.addClassRules("name", { required: true, minlength: 2 });
[4] addClassRules( rules ) 返回:undefined
參數 rules 類型:Options 樣式類名-規則表。
說明:添加一個複合的樣式驗證方法。對於重構通用的聯合規則非常有用。
- jQuery.validator.addClassRules({
- name: {
- required: true,
- minlength: 2
- },
- zip: {
- required: true,
- digits: true,
- minlength: 5,
- maxlength: 5
- }
- });
jQuery.validator.addClassRules({ name: { required: true, minlength: 2 }, zip: { required: true, digits: true, minlength: 5, maxlength: 5 } });
原文請見:http://docs.jquery.com/Plugins/Validation
------------------------------------------------------------------------------------------------------------------------------------------------------