jQuery Validate Plugin - Trigger validation of single field

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

I've got a form that can optionally be pre-populated via facebook connect. Once a user connects, their name and email are automatically filled in. The problem is that this doesn't trigger the remote validation to check if the email already exists.

Is there a way I could call the validation on that field alone? Something like:

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

would be idea. Searched through the docs with no luck.

回答1:

This method seems to do what you want:

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


回答2:

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...



回答3:

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.



回答4:

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


回答5:

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

  

-- cross posted with this similar question



回答6:

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)*/ } 


回答7:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!