Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus invalid fields with jQuery validate?

There is focusInvalid option, which is true by default. But it works only when form submission happens. If I validate the form with valid method, then it doesn't work. So the question is how to focus invalid field when valid is used?

Please see this demo to see the difference. Just press buttons there.

like image 912
LA_ Avatar asked Apr 11 '12 18:04

LA_


People also ask

How do you check if jQuery validate is working?

The plugin adds a validationPlugin function to jQuery. fn , so simply check whether it exists or not; if (typeof jQuery. fn.

What is jQuery validate unobtrusive?

An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.

What is jQuery validate used for?

We're going to use the jQuery Validation Plugin to validate our form. The basic principle of this plugin is to specify validation rules and error messages for HTML elements in JavaScript.

What is validator addMethod in jQuery?

validator. addMethod( name, method [, message ] ) Description: Add a custom validation method. It must consist of a name (must be a legal javascript identifier), a javascript based function and a default string message.


1 Answers

With the invalidHandler you can set focus to the first element that fails:

$("#form").validate({     onfocusout: false,     invalidHandler: function(form, validator) {         var errors = validator.numberOfInvalids();         if (errors) {                                 validator.errorList[0].element.focus();         }     }  }); 
like image 171
James Johnson Avatar answered Nov 01 '22 20:11

James Johnson