![](http://static.javashuo.com/static/loading.gif)
jQuery.validate.js API
![](http://static.javashuo.com/static/loading.gif)
Name Type
![](http://static.javashuo.com/static/loading.gif)
validate( options ) Returns: Validator
![](http://static.javashuo.com/static/loading.gif)
驗證所選的FORM
![](http://static.javashuo.com/static/loading.gif)
valid( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
檢查是否驗證經過
![](http://static.javashuo.com/static/loading.gif)
rules( ) Returns: Options
![](http://static.javashuo.com/static/loading.gif)
返回元素的驗證規則
![](http://static.javashuo.com/static/loading.gif)
rules(
"add", rules ) Returns: Options
![](http://static.javashuo.com/static/loading.gif)
增長驗證規則。
![](http://static.javashuo.com/static/loading.gif)
rules(
"remove", rules ) Returns: Options
![](http://static.javashuo.com/static/loading.gif)
刪除驗證規則
![](http://static.javashuo.com/static/loading.gif)
removeAttrs( attributes ) Returns: Options
![](http://static.javashuo.com/static/loading.gif)
刪除特殊屬性而且返回他們
![](http://static.javashuo.com/static/loading.gif)
Custom selectors
![](http://static.javashuo.com/static/loading.gif)
Name TypeCustom selectors:
![](http://static.javashuo.com/static/loading.gif)
Name Type
![](http://static.javashuo.com/static/loading.gif)
:blank Returns: Array <Element >
![](http://static.javashuo.com/static/loading.gif)
沒有值的篩選器
![](http://static.javashuo.com/static/loading.gif)
:filled Returns: Array <Element >
![](http://static.javashuo.com/static/loading.gif)
有值的篩選器
![](http://static.javashuo.com/static/loading.gif)
:
unchecked Returns: Array <Element >
![](http://static.javashuo.com/static/loading.gif)
沒選擇的元素的篩選器
![](http://static.javashuo.com/static/loading.gif)
Utilities
![](http://static.javashuo.com/static/loading.gif)
Name TypeString utilities:
![](http://static.javashuo.com/static/loading.gif)
Name Type
![](http://static.javashuo.com/static/loading.gif)
jQuery.format( template, argument , argumentN... ) Returns: String
![](http://static.javashuo.com/static/loading.gif)
用參數代替模板中的 {n}。
![](http://static.javashuo.com/static/loading.gif)
Validator
![](http://static.javashuo.com/static/loading.gif)
validate方法返回一個Validator對象, 它有不少方法, 讓你能使用引起校驗程序或者改變form的內容. validator對象有不少方法, 但下面只是列出經常使用的.
![](http://static.javashuo.com/static/loading.gif)
Name TypeValidator methods:
![](http://static.javashuo.com/static/loading.gif)
Name Type
![](http://static.javashuo.com/static/loading.gif)
form( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證form返回成功仍是失敗
![](http://static.javashuo.com/static/loading.gif)
element( element ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證單個元素是成功仍是失敗
![](http://static.javashuo.com/static/loading.gif)
resetForm( ) Returns: undefined
![](http://static.javashuo.com/static/loading.gif)
把前面驗證的FORM恢復到驗證前原來的狀態
![](http://static.javashuo.com/static/loading.gif)
showErrors( errors ) Returns: undefined
![](http://static.javashuo.com/static/loading.gif)
顯示特定的錯誤信息
![](http://static.javashuo.com/static/loading.gif)
There are a few
static methods on the validator
object:
![](http://static.javashuo.com/static/loading.gif)
Name TypeValidator functions:
![](http://static.javashuo.com/static/loading.gif)
Name Type
![](http://static.javashuo.com/static/loading.gif)
setDefaults( defaults ) Returns: undefined
![](http://static.javashuo.com/static/loading.gif)
改變默認的設置
![](http://static.javashuo.com/static/loading.gif)
addMethod( name, method, message ) Returns: undefined
![](http://static.javashuo.com/static/loading.gif)
添加一個新的驗證方法. 必須包括一個獨一無二的名字,一個JAVASCRIPT的方法和一個默認的信息
![](http://static.javashuo.com/static/loading.gif)
addCla***ules( name, rules ) Returns: undefined
![](http://static.javashuo.com/static/loading.gif)
增長組合驗證類型 在一個類裏面用多種驗證方法裏比較有用
![](http://static.javashuo.com/static/loading.gif)
addCla***ules( rules ) Returns: undefined
![](http://static.javashuo.com/static/loading.gif)
增長組合驗證類型 在一個類裏面用多種驗證方法裏比較有用,這個是一會兒加多個[edit ]
![](http://static.javashuo.com/static/loading.gif)
List of built-
in Validation methods
![](http://static.javashuo.com/static/loading.gif)
A set of standard validation methods
is provided:
![](http://static.javashuo.com/static/loading.gif)
Name TypeMethods:
![](http://static.javashuo.com/static/loading.gif)
Name Type
![](http://static.javashuo.com/static/loading.gif)
required( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
必填驗證元素
![](http://static.javashuo.com/static/loading.gif)
required( dependency-expression ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
必填元素依賴於表達式的結果.
![](http://static.javashuo.com/static/loading.gif)
required( dependency-callback ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
必填元素依賴於回調函數的結果.
![](http://static.javashuo.com/static/loading.gif)
remote( url ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
請求遠程校驗。url一般是一個遠程調用方法
![](http://static.javashuo.com/static/loading.gif)
minlength( length ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
設置最小長度
![](http://static.javashuo.com/static/loading.gif)
maxlength( length ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
設置最大長度
![](http://static.javashuo.com/static/loading.gif)
rangelength( range ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
設置一個長度範圍[min,max]
![](http://static.javashuo.com/static/loading.gif)
min( value ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
設置最小值.
![](http://static.javashuo.com/static/loading.gif)
max( value ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
設置最大值.
![](http://static.javashuo.com/static/loading.gif)
range( range ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
設置值的範圍
![](http://static.javashuo.com/static/loading.gif)
email( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證電子郵箱格式
![](http://static.javashuo.com/static/loading.gif)
url( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證鏈接格式
![](http://static.javashuo.com/static/loading.gif)
date( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證日期格式(相似30/30/2008的格式,不驗證日期準確性只驗證格式)
![](http://static.javashuo.com/static/loading.gif)
dateISO( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
研製ISO類型的日期格式
![](http://static.javashuo.com/static/loading.gif)
dateDE( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證德式的日期格式(29.04.1994 or 1.1.2006)
![](http://static.javashuo.com/static/loading.gif)
number( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證十進制數字(包括小數的)
![](http://static.javashuo.com/static/loading.gif)
numberDE( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
Makes the element require a
decimal number with german format.
![](http://static.javashuo.com/static/loading.gif)
digits( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證整數
![](http://static.javashuo.com/static/loading.gif)
creditcard( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證信用卡號
![](http://static.javashuo.com/static/loading.gif)
accept( extension ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證相同後綴名的字符串
![](http://static.javashuo.com/static/loading.gif)
equalTo( other ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證兩個輸入框的內容是否相同
![](http://static.javashuo.com/static/loading.gif)
Name Type
![](http://static.javashuo.com/static/loading.gif)
phoneUS( ) Returns: Boolean
![](http://static.javashuo.com/static/loading.gif)
驗證美式的電話號碼
![](http://static.javashuo.com/static/loading.gif)
validate ()的可選項
![](http://static.javashuo.com/static/loading.gif)
debug:進行調試模式
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate
![](http://static.javashuo.com/static/loading.gif)
({
![](http://static.javashuo.com/static/loading.gif)
debug:
true
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
把調試設置爲默認
![](http://static.javashuo.com/static/loading.gif)
$.validator.setDefaults({
![](http://static.javashuo.com/static/loading.gif)
debug:
true
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
submitHandler:用其餘方式替代默認的SUBMIT,好比用AJAX的方式提交
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
submitHandler: function(form) {
![](http://static.javashuo.com/static/loading.gif)
$(form).ajaxSubmit();
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
ignore:忽略某些元素不驗證
![](http://static.javashuo.com/static/loading.gif)
$(
"#myform").validate({
![](http://static.javashuo.com/static/loading.gif)
ignore:
".ignore"
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
rules: 默認下根據form的classes, attributes, metadata判斷,但也能夠在validate函數裏面聲明
![](http://static.javashuo.com/static/loading.gif)
Key/value 可自定義規則. Key是對象的名字 value是對象的規則,能夠組合使用
class/attribute/metadata rules.
![](http://static.javashuo.com/static/loading.gif)
如下代碼特別驗證selector類中name='name'是必填項而且 email是既是必填又要符合email的格式
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
rules: {
// simple rule, converted to {required:true}
![](http://static.javashuo.com/static/loading.gif)
name:
"required",
// compound rule
![](http://static.javashuo.com/static/loading.gif)
email: {
![](http://static.javashuo.com/static/loading.gif)
required:
true,
![](http://static.javashuo.com/static/loading.gif)
email:
true
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
messages:更改默認的提示信息
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
rules: {
![](http://static.javashuo.com/static/loading.gif)
name:
"required",
![](http://static.javashuo.com/static/loading.gif)
email: {
![](http://static.javashuo.com/static/loading.gif)
required:
true,
![](http://static.javashuo.com/static/loading.gif)
email:
true
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
},
![](http://static.javashuo.com/static/loading.gif)
messages: {
![](http://static.javashuo.com/static/loading.gif)
name:
"Please specify your name",
![](http://static.javashuo.com/static/loading.gif)
email: {
![](http://static.javashuo.com/static/loading.gif)
required:
"We need your email address to contact you",
![](http://static.javashuo.com/static/loading.gif)
email:
"Your email address must be in the format of name@domain.com"
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
groups:定義一個組,把幾個地方的出錯信息贊成放在一個地方,用error Placement控制把出錯信息放在哪裏
![](http://static.javashuo.com/static/loading.gif)
$(
"#myform").validate({
![](http://static.javashuo.com/static/loading.gif)
groups: {
![](http://static.javashuo.com/static/loading.gif)
username:
"fname lname"
![](http://static.javashuo.com/static/loading.gif)
},
![](http://static.javashuo.com/static/loading.gif)
errorPlacement: function(error, element) {
if (element.attr(
"name") ==
"fname" || element.attr(
"name") ==
"lname" )
![](http://static.javashuo.com/static/loading.gif)
error.insertAfter(
"#lastname");
else
![](http://static.javashuo.com/static/loading.gif)
error.insertAfter(element);
![](http://static.javashuo.com/static/loading.gif)
},
![](http://static.javashuo.com/static/loading.gif)
debug:
true
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
nsubmit Boolean Default:
true
![](http://static.javashuo.com/static/loading.gif)
提交時驗證. 設置惟
false就用其餘方法去驗證
![](http://static.javashuo.com/static/loading.gif)
Code
![](http://static.javashuo.com/static/loading.gif)
不用onsubmit驗證,就容許用戶不管用什麼方法去驗證,而是提交時, 用 keyup/blur/click 等方法.
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
onsubmit:
false
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
onfocusout Boolean Default:
true
![](http://static.javashuo.com/static/loading.gif)
Validate elements (except checkboxes/radio buttons) on blur. If nothing
is entered, all rules are skipped, except when the field was already marked
as invalid.
![](http://static.javashuo.com/static/loading.gif)
Code
![](http://static.javashuo.com/static/loading.gif)
Disables onblur validation.
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
onfocusout:
false
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
onkeyup Boolean Default:
true
![](http://static.javashuo.com/static/loading.gif)
在keyup時驗證. As
long
as the field
is not marked
as invalid, nothing happens. Otherwise, all rules are
checked on each key up
event.
![](http://static.javashuo.com/static/loading.gif)
Code
![](http://static.javashuo.com/static/loading.gif)
Disables onkeyup validation.
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
onkeyup:
false
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
onclick Boolean Default:
true
![](http://static.javashuo.com/static/loading.gif)
在checkboxes 和 radio 點擊時驗證.
![](http://static.javashuo.com/static/loading.gif)
Code
![](http://static.javashuo.com/static/loading.gif)
Disables onclick validation of checkboxes and radio buttons.
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
false
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
focusInvalid Boolean Default:
true
![](http://static.javashuo.com/static/loading.gif)
把焦點聚焦在最後一個動做或者最近的一次出錯上via validator.focusInvalid(). The last active element
is the one that had focus when the form was submitted, avoiding to steal its focus. If there was no element focused, the first one
in the form gets it, unless
this option
is turned off.
![](http://static.javashuo.com/static/loading.gif)
Code
![](http://static.javashuo.com/static/loading.gif)
Disables focusing of invalid elements.
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
focusInvalid:
false
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
focusCleanup Boolean Default:
false
![](http://static.javashuo.com/static/loading.gif)
若是是
true那麼刪除出錯類從出錯的元素上而且隱藏出錯信息當這個元素被聚焦 .避免和 focusInvalid.一塊兒用
![](http://static.javashuo.com/static/loading.gif)
Code
![](http://static.javashuo.com/static/loading.gif)
Enables cleanup when focusing elements, removing the error
class and hiding error messages when an element
is focused.
![](http://static.javashuo.com/static/loading.gif)
$(
".selector").validate({
![](http://static.javashuo.com/static/loading.gif)
focusCleanup:
true
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
meta String
![](http://static.javashuo.com/static/loading.gif)
爲了元數據使用其餘插件你要包裝 你的驗證規則 在他們本身的項目中能夠用這個特殊的選項
![](http://static.javashuo.com/static/loading.gif)
Tell the validation plugin to look inside a validate-property
in metadata
for validation rules.
![](http://static.javashuo.com/static/loading.gif)
$(
"#myform").validate({
![](http://static.javashuo.com/static/loading.gif)
meta:
"validate",
![](http://static.javashuo.com/static/loading.gif)
submitHandler: function() { alert(
"Submitted!") }
![](http://static.javashuo.com/static/loading.gif)
})
![](http://static.javashuo.com/static/loading.gif)
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<mce:script src="http://code.jquery.com/jquery-latest.js" mce_src="http://code.jquery.com/jquery-latest.js"></mce:script>
<mce:script type="text/javascript"><!--
$(document).ready(function(){
$("#myform").validate({
meta: "validate",
submitHandler: function() { alert("Submitted!") }
})
});
// --></mce:script>
</head>
<body>
<mce:script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/lib/jquery.metadata.js" mce_src="http://dev.jquery.com/view/trunk/plugins/validate/lib/jquery.metadata.js"></mce:script>
<mce:script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js" mce_src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></mce:script>
<form id="myform">
<input type="text" name="email" class="{validate:{ required: true, email:true }}" />
<br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
errorClass String Default: "error"
建立錯誤類的名字爲了去尋找存在的錯誤標籤和增長它到驗證失敗的元素中去。
Code
Sets the error class to "invalid".
$(".selector").validate({
errorClass: "invalid"
})
errorElement String Default: "label"
設置錯誤的元素,默認的是label你能夠改爲em.Use this element type to create error messages and to look for existing error messages. The default, "label", has the advantage of creating a meaningful link between error message and invalid field using the for attribute (which is always used, no matter the element type).
Code
Sets the error element to "em".
$(".selector").validate
errorElement: "em"
})
wrapper String
在出錯信息外用其餘的元素包裝一層。Wrap error labels with the specified element. Useful in combination with errorLabelContainer to create a list of error messages.
Code
Wrap each error element with a list item, useful when using an ordered or unordered list as the error container.
$(".selector").validate({
wrapper: "li"
})
errorLabelContainer Selector
把錯誤信息統一放在一個容器裏面。Hide and show this container when validating.
All error labels are displayed inside an unordered list with the ID "messageBox", as specified by the selector passed as errorContainer option. All error elements are wrapped inside an li element, to create a list of messages.
$("#myform").validate({
errorLabelContainer: "#messageBox",
wrapper: "li",
submitHandler: function() { alert("Submitted!") }
})