//---------- [one.ngn] Content Managent System -------------- //---------- created by zerophobic.com -------------- //----------------------------------------------------------- // MODULE : validation javascript // FUNCTION : performs form validation // AUTHOR : jim richards // CREATED : 01/10/2001 // MODIFIED : 11/01/2002 //----------------------------------------------------------- //----------------------------------------------------------- /* start form validation code */ // returns true if all the characters in value are in str function isOnly(value, str) { // check if character 'c' from value is in str for (var i = 0 ; i < value.length ; i++) { var c = value.charAt(i); if (str.indexOf(c) == -1) return false; } return true; } function isValid(value){ return alphaNumeric(value); } function binary(value){ return alphaNumeric(value); } function select(value){ return true; } // function to test for only letters or numbers function alphaNumeric(value) { // for for null and empty if ((value == null) || (value == "")) return false; // check for all blank return !isOnly(value, " \t\n"); } // this checks for only numbers function numeric(value) { if (!isValid(value)) return false; return isOnly(value, "0123456789"); } function isValidCreditMonth(value) { if (!isValid(value)) return false; return isOnly(value, "0123456789") && value.length == 2 && 1 <= value && value <= 12; } function isValidCreditYear(value) { if (!isValid(value)) return false; // tricky as year is NOT Y2K at all, have a 2 digit value between 00 and 99 return isOnly(value, "0123456789") && value.length == 2 && 0 <= value && value <= 99; } function isValidCreditExpiry(value) { if (!isValid(value)) return false; // tricky as year is NOT Y2K at all, have a 2 digit value between 00 and 99 return isOnly(value, "0123456789/") && value.length == 5 && value.substring(2,3) == "/"; } function isValidCreditCard(value) { if (!isValid(value)) return false; // tricky as year is NOT Y2K at all, have a 2 digit value between 00 and 99 return isOnly(value, "0123456789"); } // this checks for a phone valid number function phoneNumber(value) { if (!isValid(value)) return false; // now check for a number, perhaps with '-', '(', ')' and '+' in it ' ' return isOnly(value, "-|+0123456789() "); } // this checks for a valid username function username(value) { if (!isValid(value)) return false; // Check that length is 4-12. // Check that charset is valid. return ((value.length >= 4) && (value.length <= 20)) && (isOnly(value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_")) } function password(value) { if (!isValid(value)) return false; // Check that length is 4-15. if ((value.length >= 4) && (value.length <= 15)){ // check that there is at least one numeral var str = "0123456789"; for (var i = 0 ; i < value.length ; i++) { var c = value.charAt(i); if (str.indexOf(c) != -1) return true; } } else { return false; } } function emailAddress(value) { try { if (!isValid(value)) return false; // sourced from http://www.breakingpar.com/bkp/home.nsf/Doc?OpenNavigator&U=87256B280015193F87256C40004CC8C6 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ return re.test(value); //var at_sign = value.indexOf('@'); //return at_sign != -1 && at_sign != 0 && at_sign != value.length; } catch (er) { alert('You are using an old browser and we could not validate your email address. Please verify that it is correct before submitting your order.'); return true; } } function validate(data_form) { // assume valid, until shown otherwise var valid_form = true; // the error message string for display var empty_elements = ""; // the first empty element var first_element = null; // loop through the form looking for fields that are required // as these are required fields and must have input. for (var i = 0 ; i < data_form.length ; i++) { var element = data_form.elements[i]; // the data is invalid if either // element is not required, something is supplied and it has a validation routine it fails // or // element is required, it has a validation routine it fails or it fails the default one if( (!element.required && (isValid(element.value) && element.validate != null && !element.validate(element.value))) || (element.required && ((element.validate != null && !element.validate(element.value)) || !isValid(element.value))) ){ // so the form doesn't submit valid_form = false; // for the error message (clean it up for display) empty_elements += " - " + element.display_name + "\n"; // set the first element that is empty for focus (and not hidden) if (first_element == null && (element.type != "hidden")) first_element = element; } } if (!valid_form) { var error_message = ""; // build the error message if (empty_elements != "") error_message = "These fields do not have correct values:\n" + empty_elements; // and display for all to see alert(error_message); // and jump to the first element if (first_element != null) first_element.focus(); } // if cookie code is being used, the call to cookie.save() is made here // if there are no errors in the validation of the form // if (valid_form) // save_data(); return valid_form; } /* end form validation code */