var validationHandlers = new Array(); function register_validation_handler ( validation_handler ) { // Add `function` to the list of functions that will be called when validating // the page. validationHandlers.push( validation_handler ); return validation_handler; } function validate_form () { /* Global function for validating a form. This examines the validationHandlers * array and produces an aggregate result. If there were failures then the user * is prompted and the function returns false else it returns true. * * This function is the only intended interface to the validation functions here * beyond the register_validation_handler( ... ) function. */ // Get an object to hold everything for us. var overall = new ValidationResult(); // Loop through each installed validation handler and add the individual result to the // cumulative total. for (var ix = 0; ix < validationHandlers.length; ix++) { // Retrieve each handler in turn. var handler = validationHandlers[ ix ]; // And execute it. var result = handler(); // Maintain an overall failure state if (result.failed) overall.failed = result.failed; // Copy over the focus field if it hasn't been set before if (overall.focus_field == null && result.focus_field != null) overall.focus_field = result.focus_field; // Pass in the bad field failure messages. overall.bad_fields = overall.bad_fields.concat( result.bad_fields ); // And any other errors. overall.errors = overall.errors.concat( result.errors ); } // Validation is now complete. // Generate a 'bad fields' message if there are any. if (overall.bad_fields[0] != null) { if (overall.bad_fields.length > 1) overall.error("Please check the following fields for accuracy:\n"+ overall.bad_fields.join("\n") ); else overall.error("Please check the " + overall.bad_fields[0] + " field for accuracy."); } // Report any and all error messages if (overall.errors[0] != null) alert(overall.errors.join("\n\n")); // Since success is the opposite of failure... negate failure to return the success value. return ! overall.failed } function ValidationResult () { /* * The ValidationResult constructor. * For normal validation see the function validate_form(). * * Execute ` ... = new ValidationResult(); ` to create a new object. * * By default, validation objects are considered to have passed muster. Your validation * code is expected to signal a failure by logging an error message with error(...) or bad data * with bad_field( ... ). * * The end result can be examined by looking at the failed property. */ // Create a new generic object var result = new Object; // Set all of the properties to their default values. result.failed = false; result.bad_fields = new Array(); result.errors = new Array(); result.exit_immediately = false; result.focus_field = null; // Install methods into the new object. result.bad_field = validation_bad_field_method; result.error = validation_error_method; result.debug = validation_debug_method; result.debug = validation_debug_method; return result; } function validation_bad_field_method ( field_name, message ) { // Mark this field as a failure with a message if ( this.focus_field == null) this.focus_field = field_name; // Do some auto-defaulting. if ( message == null || message == '' ) message = field_name; this.bad_fields.push( message ); this.failed = true; return this; } function validation_error_method ( message ) { // Add 'message' to the list of errors, mark the object as failing validation this.errors.push( message ); this.failed = true; return this; }; function validation_debug_method () { // Prompt the user with the internal state of the validation object. alert( 'failed:'+this.failed+ "\nbad_fields:"+this.bad_fields.join(",")+ "\nerrors:"+this.errors.join(",")+ "\nexit_immediately:"+this.exit_immediately+ "\nfocus_field:"+this.focus_field); return this; }