NAME Form.Validator - a class for form validation SYNOPSIS Make a form with an "onSubmit" handler:
Add in JavaScript later on: DESCRIPTION This class provides a simple, object-oriented API for form validation. After instantiating a new Form.Validator object, form validation is just a matter of setting up a list of constraints for your form inputs using the "set()" method. Then, in the onSubmit handler for your form, "return fv.validateAndAlert()". So far, only two validation functions are provided: notZero and notBlank. It might not seem like much, but you can get surprisingly far with them. See examples/payment_profile.mas.html to see just how far. Of course, you may use custom validation functions as well. See "WRITING YOUR OWN VALIDATION FUNCTIONS" to find out how. METHODS new Form.Validator(form) This is the constructor. It takes a form object as its only parameter. Example: var fv = new Form.Validator(document.stuff); var fv2 = new Form.Validator(document.forms[0]); set('input_field', validationFunction, 'Error Message'); This method associates a validation function and error message to an input field in the form. Example: // using a built-in validator function fv.set('name', 'notBlank', "Name must be filled in."); // using a custom validator fv.set('category_id', function(fv, field) { if (fv.form[field].value == 3) { return false; } else { return true; } }, "Problems always come in 3s. Bad choice, my friend." ); get('input_field') This method returns a list of the validation settings for a given input field. validate() This method runs through the list of validation settings in the order they were defined and returns an Array of boolean values which indicate whether the test passed or not. "True" means it passed, and "false" means it failed. validateAndAlert() This function is intended to be used as an "onSubmit" handler on a form. It runs through the validation tests in the order they were defined and displays an alert box if any of the validation tests failed. Then it will return "false" to prevent the form from being submitted. Example: