Validatious 2.0

Easy form validation with unobtrusive JavaScript

Scoping validators

All the previous examples will without any extra configuration validate the entire form on submit, and then revalidate each field when they are updated.

What if your form has several buttons - only one or a few of which should trigger validation? An example could be a processes with Next, Previous and Cancel buttons - only the next button should probably trigger validation.

Validatious can scope validation to specific buttons only. You can do this by calling the on method on the form facade object, which is returned by validate, validateAll and validateAny. This method takes arbitrary many arguments, allowing you to add several buttons at the same time. The values should be either an element, or the element id. The element itself may be the button (ie the input element), or the input element's parent (accomodating the usual practice of wrapping an input in a div to create custom buttons).

validate( /* ... */).on("next");
validate( /* ... */).on("next", "save");
validate( /* ... */).on(document.getElementById("next"), v2.$("save"));
// And so on...

Example

Try clicking the next button to see the validation kick in - then click previous or cancel to see the form submit even if the fields are invalid.


Next: Creating compound validation logic | Back to DSL features index

© 2008 Christian Johansen. Validatious is licensed with a BSD License, documentation and site content with CC Attribution-Share Alike. About.