Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire an event on error in jquery validator plugin

I have jquery validator running and the page is very long, so I want the page to scroll up to the top because I display errors on the very top of the page above the form, does anyone know where I can put the code for the animation so it fires when the form has errors?

like image 746
Omar Abdallah Avatar asked May 21 '10 20:05

Omar Abdallah


People also ask

How do I show different errors in jQuery validator addMethod?

validator. addMethod('min-length', function (val, element) { // do stuff // the error message here needs to be dynamic }, 'The field cannot be less than than ' + element. attr('data-min') + // it is within the closure, but it can't grab it ' length. ');

What is valid () in jQuery?

valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.

What is validator addMethod in jQuery?

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.

What is jQuery validation engine?

jQuery validation engine is a Javascript plugin aimed at the validation of form fields in the browser (IE 6-8, Chrome, Firefox, Safari, Opera 10). The plugin provides visually appealing prompts that grab user attention on the subject matter.


1 Answers

You can use the invalidHandler option for the exact case you want, like this:

$("form").validate({
  rules: { ... rules here ... }
  invalidHandler: function(form, validator) {
    $('html, body').animate({scrollTop: '0px'}, 300);      
  }
});
like image 196
Nick Craver Avatar answered Nov 04 '22 23:11

Nick Craver