Name | Type |
---|---|
validate( options ) | Returns: Validator |
验证所选的FORM | |
valid( ) | Returns: Boolean |
检查是否验证通过 |
|
rules( ) | Returns: Options |
返回元素的验证规则 | |
rules( "add", rules ) | Returns: Options |
增加验证规则。 | |
rules( "remove", rules ) | Returns: Options |
删除验证规则 | |
removeAttrs( attributes ) | Returns: Options |
删除特殊属性并且返回他们 |
validate方法返回一个Validator对象, 它有很多方法, 让你能使用引发校验程序或者改变form的内容. validator对象有很多方法, 但下面只是列出常用的.
There are a few static methods on the validator object:
A set of standard validation methods is provided:
Name | Type |
---|---|
phoneUS( ) | Returns: Boolean |
验证美式的电话号码 |
$(".selector").validate
({
debug: true
})
把调试设置为默认
$ .validator.setDefaults ({
debug: true
})
submitHandler:用其他方式替代默认的SUBMIT,比如用AJAX的方式提交
$ (".selector").validate ({
submitHandler: function(form) {
$ (form).ajaxSubmit();
}
})
ignore:忽略某些元素不验证
$ ("#myform").validate ({
ignore: ".ignore"
})
rules: 默认下根据form的classes, attributes, metadata判断,但也可以在validate函数里面声明
Key/value 可自定义规则. Key是对象的名字 value是对象的规则,可以组合使用 class/attribute/metadata rules.
以下代码特别验证selector类中name='name'是必填项并且 email是既是必填又要符合email的格式
$ (".selector").validate ({
rules: {
// simple rule, converted to {required:true}
name: "required",
// compound rule
email: {
required: true,
email: true
}
}
})
messages:更改默认的提示信息
$ (".selector").validate ({
rules: {
name: "required",
email: {
required: true,
email: true
}
},
messages: {
name: "Please specify your name",
email: {
required: "We need your email address to contact you",
email: "Your email address must be in the format of [email protected]"
}
}
})
groups:定义一个组,把几个地方的出错信息同意放在一个地方,用
Placement控制把出错信息放在哪里$ ("#myform").validate ({
groups: {
username: "fname lname"
},
Placement: function( , element) {
if (element.attr("name") == "fname" || element.attr("name") == "lname" )
.insertAfter("#lastname");
else
.insertAfter(element);
},
debug:true
})
nsubmit | Boolean | Default: true |
---|---|---|
提交时验证. 设置唯false就用其他方法去验证 | ||
不用onsubmit验证,就允许用户无论用什么方法去验证,而是提交时, 用 keyup/blur/click 等方法.
$ (".selector").validate ({ |
||
onfocusout | Boolean | Default: true |
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. | ||
Disables onblur validation.
$ (".selector").validate ({ |
||
onkeyup | Boolean | Default: true |
在keyup时验证. As long as the field is not marked as invalid, nothing happens. Otherwise, all rules are checked on each key up event. | ||
Disables onkeyup validation.
$ (".selector").validate ({ |
||
onclick | Boolean | Default: true |
在checkboxes 和 radio 点击时验证. | ||
Disables onclick validation of checkboxes and radio buttons.
$ (".selector").validate ({ |
||
focusInvalid | Boolean | Default: true |
把焦点聚焦在最后一个动作或者最近的一次出错上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. | ||
Disables focusing of invalid elements.
$ (".selector").validate ({ |
||
focusCleanup | Boolean | Default: false |
如果是true那么删除出错类从出错的元素上并且隐藏出错信息当这个元素被聚焦 .避免和 focusInvalid.一起用 | ||
Enables cleanup when focusing elements, removing the error class and hiding error messages when an element is focused.
$ (".selector").validate ({ |
||
meta | String | |
为了元数据使用其他插件你要包装 你的验证规则 在他们自己的项目中可以用这个特殊的选项 | ||
Tell the validation plugin to look inside a validate-property in metadata for validation rules.
$ ("#myform").validate ({ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||
errorClass | String | Default: "error" |
创建错误类的名字为了去寻找存在的错误标签和增加它到验证失败的元素中去。 | ||
Sets the error class to "invalid".
$ (".selector").validate ({ |
||
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). | ||
Sets the error element to "em".
$ (".selector").validate |
||
wrapper | String | |
在出错信息外用其他的元素包装一层。Wrap error labels with the specified element. Useful in combination with errorLabelContainer to create a list of error messages. | ||
Wrap each error element with a list item, useful when using an ordered or unordered list as the error container.
$ (".selector").validate ({ |
||
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 ({ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd "> |
||
errorContainer | Selector | |
显示或者隐藏验证信息 | ||
使用一个额外的容器去显示错误信息
Uses an additonal container for error messages. The elements given as the errorContainer are all shown and hidden when errors occur. But the error labels themselve are added to the element(s) given as errorLabelContainer, here an unordered list. Therefore the error labels are also wrapped into li elements (wrapper option).
$ ("#myform").validate ({ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||
showErrors | Callback | Default: None, uses built-in message disply. |
得到错误的显示句柄 Gets the map of errors as the first argument and and array of errors as the second, called in the context of the validator object. The arguments contain only those elements currently validated, which can be a single element when doing validation onblur/keyup. You can trigger (in addition to your own messages) the default behaviour by calling this.defaultShowErrors(). |
||
Update the number of invalid elements each time an error is displayed. Delegates to the default implementation for the actual error display.
$ (".selector").validate ({ |
||
errorPlacement | Callback | Default: 把错误label放在验证的元素后面 |
可选错误label的放置位置. First argument: The created error label as a jQuery object. Second argument: The invalid element as a jQuery object. | ||
Use a table layout for the form, placing error messags in the next cell after the input.
$ ("#myform").validate ({ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||
success | String , Callback | |
成功时的class.If specified, the error label is displayed to show a valid element. If a String is given, its added as a class to the label. If a Function is given, its called with the label (as a jQuery object) as its only argument. That can be used to add a text like "ok!". | ||
添加"valid" 到验证验证元素, 在CSS中定义的样式
$ ("#myform").validate ({ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||
添加"valid" 到验证验证元素, 在CSS中定义的样式,并加入“ok”的文字
$ ("#myform").validate ({ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||
highlight | Callback | Default: Adds errorClass (see the option) to the Element |
高亮显示错误信息。 或者说重写出错时的出错元素的显示。Override to decide which fields and how to highlight. | ||
Highlights an invalid element by fading it out and in again.
$ (".selector").validate ({ |
||
Adds the error class to both the invalid element and it's label
$ (".selector").validate ({ |
||
unhighlight | Callback | Default: 默认是去掉error类 |
Called to revert changes made by option highlight, same arguments as highlight. |