This is a simple validator script I wrote up. It does automatic validation of specified forms. No method calls are required to set it up; it automatically binds itself to designated forms.
Simply tag any form that you want validated with class "validate". Then, tag form elements with classes to determine how said elements are validated. The built-in rules are:
requiredphonezipemailrequired tag can be combined with any of the others. For example, a field tagged "required zip" will pass only if it is given a valid zip code. A field simply tagged "zip" will pass if the field is either empty or given a valid zip code.
Inputs that fail validation are tagged with class "failed".
To add a new rule to the validator, attach a callback to the Validator.rules object. The return value of the callback specifies the validity of a field tagged with that name. For example, assigning a callback to Validator.rules.address will cause that callback to be fired whenever a field of class address is encountered.
The validator is automatically bound to the submit event of every form of class validate. However, to call the validator manually, supply a selector string (e.g. form#my-form) to Validator.validate().
If validation fails, a collection of elements that failed the validation is stored in the invalidFields data field of the form element in question. Use $(formSelector).data("invalidFields") to retrieve it. This collection is cleared if the form later passes validation.