/* quirksmode.org based library version 1.0 created 2005-12-18 by gc last modified 2006-02-08 by gc */ //------------------------------------------------------------------------------------------------------------------------------------------------ // VARS FOR YOU TO SET // messages var required_field_error_message = 'This field is required.'; var invalid_numeric_error_message = 'The value of this field must be a number.'; var invalid_numeric_range_message = 'The value of this field must be'; var invalid_email_error_message = 'This is not a valid email address.'; var invalid_form_message = 'This form cannot be submitted. Please check the page for error messages.'; // form element names var input_error_td = 'error_td'; // this is the name of the hidden element that indicates if errors should change the appearance of a . The value is 1 var input_required_fields = 'required_fields'; // this is the name of the hidden element that indicates if the form contains required fields. The value of the input is a comma separtaed list with no spaces (value="item_1,item_2,item_3") var input_numeric_fields = 'numeric_fields'; // this is the name of the hidden element that indicates if the form contains fields where the value must be number. The value of the input is a comma separtaed list with no spaces (value="item_1,item_2,item_3") var input_email_fields = 'email_fields'; // this is the name of the hidden element that indicates if the form contains fields with email adresses that need to be validated. The value of the input is a comma separtaed list with no spaces (value="item_1,item_2,item_3") var input_numeric_range = 'numeric_range'; // this is the name of the hidden element used to set a min and max value for a given numeric field. Numeric_range is an array, one element for each field to be tested. The value of each element id a comma separated list: element name, min value, max value (ie "field_name,0,10" ). That would test the input named field_name for a value equal to or greater than zero. And less than or equal to 10. Use the string 'none' to indicate that there is no min or max value // css vars var css_text_error = 'formError'; // required var css_td_error = 'formErrorTdBgColor'; // optional: use if you want the to change appearance. Use in conjunction with 'input_error_td' //================================================== // DO NOT EDIT BELOW THIS LINE //================================================== // this var tests the capabilitities of the browsers var W3CDOM_validate_forms = (document.getElementsByTagName && document.createElement); // the name of the var is a bit long due to the fact that 'W3CDOM' name is used in other scripts // static vars that get values in the functions var validForm; var firstError; var errorstring; var change_td_color = false; // should the color of the table cell change //------------------------------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------------------------------ function validateForm(the_form) { validForm = true; // assume the form is valid firstError = null; // this will be the first element in the form that in not valid, if any. This var will allow the script to set the focus on that element errorstring = ''; // a message used for non-W3C DOM browsers // should errors trigger a change in td color? // if the input_error_td form element is present if (the_form[input_error_td]) { change_td_color = true; } // validate required fields // if there are required fields if (the_form[input_required_fields]) { validateFields_Required(the_form); } // validate numeric fields // if there are numeric fields if (the_form[input_numeric_fields]) { validateFields_Numeric(the_form) } // validate numeric range values var numeric_range_name = input_numeric_range + '[0]'; // the format of the first numeric range input name is the value of input_numeric_range plus [0], "numeric_range[0]". Remeber, this is a string, not really an array if (the_form[numeric_range_name]) { validateFields_IsInNumericRange(the_form); } // validate email fields // if there are email fields var email_fields = new Array(); if (the_form[input_email_fields]) { // the form passed can have a hidden field with a comma separated list of email fields to validate // split the value on the commas and pass that array email_fields = the_form[input_email_fields].value.split(','); validateFields_Email( the_form, email_fields ); } else if (the_form.email) { // most forms just have one email field to validate. Look for an element named 'email' email_fields[0] = the_form.email.name; validateFields_Email( the_form, email_fields ); } // if the browser is old, just alert a string if (!W3CDOM_validate_forms) { alert(errorstring); } /* // this sets the focus of a form element, causing the cursor to move to the element. It is a bit buggy if (firstError) { //firstError.focus(); } */ // alert a general message to indicate the form cannot be submitted if (!validForm) { alert(invalid_form_message); } // return true or false return validForm; } //------------------------------------------------------------------------------------------------------------------------------------------------ function validateFields_Required(the_form) { var required_fields = new Array(); // the form passed needs to have a hidden field with a comma separated list of fields to validate e.g. 'name,address,email' required_fields = the_form[input_required_fields].value.split(','); var limit = required_fields.length; // loop through the required fields for (var x=0; x