JQuery 验证插件-单个字段的触发器验证

我有一个表单,可以选择预填充通过 Facebook 连接。一旦用户连接,他们的名字和电子邮件会自动填写。问题是这不会触发远程验证来检查电子邮件是否已经存在。

有没有一种方法,我可以称之为验证,仅在该领域? 类似于:

$('#email-field-only').validate()

我查了所有的文件,没找到。

113736 次浏览

This method seems to do what you want:

$('#email-field-only').valid();

Edit

API has changed, see Paul's answer.

When you set up your validation, you should be saving the validator object. you can use this to validate individual fields.

<script type="text/javascript">
var _validator;
$(function () {
_validator = $("#form").validate();
});


function doSomething() {
_validator.element($('#someElement'));
}
</script>

-- cross posted with this similar question

$("#element").validate().valid()

For some reason, some of the other methods don't work until the field has been focused/blured/changed, or a submit has been attempted... this works for me.

$("#formid").data('validator').element('#element').valid();

Had to dig through the jquery.validate script to find it...

$("#FormId").validate().element('#FieldId');

Use Validator.element():

Validates a single element, returns true if it is valid, false otherwise.

Here is the example shown in the API:

var validator = $( "#myform" ).validate();
validator.element( "#myselect" );

.valid() validates the entire form, as others have pointed out. The API says:

Checks whether the selected form is valid or whether all selected elements are valid.

If you want to validate individual form field, but don't want for UI to be triggered and display any validation errors, you may consider to use Validator.check() method which returns if given field passes validation or not.

Here is example

var validator = $("#form").data('validator');
if(validator.check('#element')){
/*field is valid*/
}else{
/*field is not valid (but no errors will be displayed)*/
}

in case u wanna do the validation for "some elements" (not all element) on your form.You can use this method:

$('input[name="element-one"], input[name="element-two"], input[name="element-three"]').valid();

Hope it help everybody :)

EDITED