topical media & game development
#javascript-code-09-validate.js / js
// A function for validating all fields within a form.
// The form argument should be a reference to a form element
// The load argument should be a boolean referring to the fact that
// the validation function is being run on page load, versus dynamically
function validateForm( form, load ) {
var valid = true;
// Go through all the field elements in form
// form.elements is an array of all fields in a form
for ( var i = 0; i < form.elements.length; i++ ) {
// Hide any error messages, if they're being shown
hideErrors( form.elements[i] );
// Check to see if the field contains valid contents, or not
if ( ! validateField( form.elements[i], load ) )
valid = false;
}
// Return false if a field does not have valid contents
// true if all fields are valid
return valid;
}
// Validate a single field's contents
function validateField( elem, load ) {
var errors = [];
// Go through all the possible validation techniques
for ( var name in errMsg ) {
// See if the field has the class specified by the error type
var re = new RegExp("(^|\\s)" + name + "(\\s|
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.