JQuery Validation Plugin - check if field is valid (but does not display validation message)

I use the jQuery validation plugin and have defined a form validation function as shown below. Based on some user actions, I run the user-defined JS function, and in this function I just want to check if the email and phone fields are valid. But I do not want to check them, i.e. I donโ€™t want to show validation errors, I just need to check if their value is valid.

Something like

$('#email').isvalid(); 

I checked the element Validator method, but this checks the element, not just checking if it is valid. So, I'm looking for a way to program rules programmatically. Any help is appreciated!

The form authentication function below:

 var validator = $("#olForm").validate({ rules: { "email": { required: true, email: true }, "phone": { required: true, digits: true, minlength: 9, maxlength: 11 } } }); 
+8
jquery jquery-validate
source share
4 answers

In response to the original jsfiddle, please see this version:

http://jsfiddle.net/33PGQ/23/

This changes in three ways: first, at the bottom, for a simple logical value, the equal valid value of both fields is set (then it is warned). Because each call to valid () returns a boolean, you can simply and easily verify that all form elements are correct.

Secondly, the validate parameter is expanded using the special function showErrors, which has no internal code. This function overrides the default behavior for validation errors, so that although the internal validity of the fields can be verified and the validity of the fields is processed, the actual error text is not displayed, thereby allowing all validation rules, but eliminating the display of validation results.

Finally, although it is not explicitly requested in the initial question, I added the optional parameter โ€œonsubmit: falseโ€, which overrides the default behavior to verify the correctness of the default to prevent the form from being submitted if all validation rules have not been successfully verified. This can be removed if you want to prevent sending (I assume you will), but I think this is a useful feature to be aware of. I have included it here if you have a special case where you want the form to be submitted, even if the form validation fails.

Of course, it is worth considering the list of options to check; There are many ways you can customize the behavior of the validator according to what you want.

+9
source share

There is an undocumented check () method that does this on purpose, without stricting these fields or without detecting errors:

 var validator = $('form').validate(); if( validator.check('#email') ){ // this field is valid but no styling was applied } 
+4
source share

There is a convenient method called .checkForm() (undocumented as of September 2015), which makes it very simple. Unlike the .valid() call, this will not show form errors when called:

 // Returns a boolean $('form').validate().checkForm(); 
+2
source share

It looks like you are looking for the .valid () method. Note that you still need to run validate () on the entire form to initialize the widget.

http://docs.jquery.com/Plugins/Validation/valid

+1
source share

All Articles