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?
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. ');
valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.
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.
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.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With