Focus on jQuery Validate invalid field

Asked

Viewed 386 times

2

I am using the jQuery Validate library to do some validations on a form and would like to know if anyone knows how to make the focus stay in the field always, in case it is invalid. Currently, it exits the invalid field but does not submit until corrected. Follows code:

$(document).ready( function() {

	$("#cadastro").validate({
		// Define as regras, pega sempre pela ID. Se não passar (caso do combo) usa o Name.
		rules:{
			cracha:{
				// ordem será obrigatório (required), numérico e terá tamanho mínimo (minLength)
				required: true, number: true,  minlength: 4
			}
		},
		// Define as mensagens de erro para cada regra
		messages:{
			cracha:{
				required: "Passe o crachá! ",
				number: "Somente numeros!",
				 minlength: "Minimo 4 digitos!"
			}
		}
	});
});

Any idea?

  • 1

    It should already work by default. Try to put the option focusInvalid: true

  • It worked out.... Thank you!

1 answer

2


The Focus in the invalid fields should already be enabled by default. But you can force the activation by setting in the options, thus:

focusInvalid: true

For more information see the documentation of plugin: http://jqueryvalidation.org/validate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.