//******************************************************* // JavaScript generic form validation script // // Dave Fisher, 03-10-2001 // v1.0 // // // The return values are based on the logic: // Is there an ERROR? // yes = return true // no = return false // // To add to HTML: // // // // Example usage script is in called ValidateExample.htm // //******************************************************* // Functions: Description: // // -- require strings -- // isEmpty Check if value is empty // isNotType Checks for digits, alphanumerics etc // isWhitespace Checks for new line, tabs, spaces etc // isEmail Checks email address structure is vallid // isPasswordError Checks pwd has: // 1 uppercase char // 1 lowercase char // 1 numeric char // Only alphanumerics // minChars Check to see string has min set of chars // maxChars Check to see string does not exceed set of chars // firstAlpha Checks to make sure first two chars are // alphabetical // // -- require objects -- // isSelected Checks if selection on select box as been made // isDate Checks date is valid // isUnderAge Checks to see if date is less than 18yrs old // // isNotChecked Has the check box been checked // isNotRadioChecked Checks to see if any radiobuttons enabled // // // vars used for validating strings var digits = "0123456789"; var lowercase = "abcdefghijklmnopqrstuvwxyz"; var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var whitespace= " \t\n\r"; //******************************************************* // Generic checks //******************************************************* // Check whether string s is empty. // returns true if empty function isEmpty(s) { if((s == null) || (s.length == 0)) return true; // check for whitespace only for(var i=0; i < s.length; ++i){ var thechar = s.charAt(i); if((thechar == ' ' || thechar == '\n' || thechar == '\t') == false){ return false; } } return true; // it's just all whitespace } // Check string for whitespace function isWhitespace(s){ // Is the string 's' empty? if (isEmpty(s)) return true; for(var i=0; i < whitespace.length; ++i) if (s.indexOf(whitespace.charAt(i)) > -1) return true; // All chars are valid return false; } // check min chars exists // return true if less than minimum function minChars(s, i){ if(s.length < i) return true; return false; } // check max chars exists // return true if exceeded function maxChars(s, i){ if(s.length > i) return true; return false; } // check first 2 chars are alphanumeric function firstAlpha(s){ var test = lowercase+uppercase; var a = s.charAt(0); var b = s.charAt(1); if(test.indexOf(a) > -1 && test.indexOf(b) > -1) return false; return true; } //// // check from DOB that person is minimum age (18) // slightly bespoke function, but most financial packages // require a min age of 18. // // This run via accessing select boxes // // ! Check date is valid by running isDate before calling this function ! // function isUnderAge(pDAY, pMONTH, pYEAR){ var minAge=18; // could have a param if required var pday = pDAY.options[pDAY.selectedIndex].value; var pmonth = pMONTH.options[pMONTH.selectedIndex].value; var pyear = pYEAR.options[pYEAR.selectedIndex].value; // Ensure numeric context. // Can't use parseInt as values beginning with '0' will be treated as octal. pday = pday - 0; pmonth = pmonth - 0; pyear = pyear - 0; // get todays date var today = new Date(); var day = today.getDate(); var month = today.getMonth()+1; // some odd reason months are 0-11 var year = today.getYear()-minAge; // today xx years ago // make sure we have a 4 digit year! year += (year < 100) ? 1900 : 0 // check year is min if(year < pyear) return true; // under age // if same year, check month if(year == pyear){ if(month < pmonth) return true; if(month == pmonth){ // check day is min if(day < pday) return true; } } return false; // DoB shows age is 18+ } // Make sure a checkbox has been enabled function isNotChecked(o){ if(o.checked) return false; return true; } // Make sure a radiobutton has been enabled function isNotRadioChecked(o){ // Loop through all radio buttons, return true if none // have been checked var i=0; while(o[i]){ if(o[i].checked) return false; ++i; } return true; } //// // check if char is not in specified range // // s = string // type = AN (alphanumerics) <- default // = D (digits) // = TXT(letter) // = LC (lowercase) // = UC (uppercase) // = ANU (AN + userdefined chars) // = UD (user defined).. user_string must be specified // user_string = user specifed string of chars to check against // function isNotType(s, type, user_string){ if(isEmpty(s)) return true; var check = lowercase+uppercase+digits; if(type == 'D') check = digits; else if(type == 'TXT') check = lowercase+uppercase; else if(type == 'LC') check = lowercase; else if(type == 'UC') check = uppercase; else if(type == 'ANU') check = check+user_string; else if(type == 'UD') check = user_string; for(var i=0; i < s.length; ++i) if (check.indexOf(s.charAt(i)) == -1) return true; return false; } //// // this checks the password contains atleast 1 of the following: // uppercase, lowercase and digit function isPasswordError(s){ // check all is no more than alphanumerics and 'underscore' var check = lowercase+uppercase+digits; for(var i=0; i < s.length; ++i) if (check.indexOf(s.charAt(i)) == -1) return true; if(s.search(/[0-9]+/g)==-1) return true; if(s.search(/[a-z]+/g)==-1) return true; if(s.search(/[A-Z]+/g)==-1) return true; return false; // there was no error } //// // Date validator, based on dd,mm, yyyy via select boxes // function isDate(pDAY, pMONTH, pYEAR){ var daysinmonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // Get date varaibles dd, mm, yyyy var startday = pDAY.options[pDAY.selectedIndex].value; var startmonth = pMONTH.options[pMONTH.selectedIndex].value; var startyear = pYEAR.options[pYEAR.selectedIndex].value; // is this a leap year?? .. reset value daysinmonth[1]=28; if(LeapYear(startyear)) daysinmonth[1]=29; // have we been sensible with the number of days in this month? if (startday > daysinmonth[startmonth-1]){ alert("Only "+daysinmonth[startmonth-1]+" days in the specified month"); return true; } return false; // no errors } // make sure data is valid (i.e. no 31/02/2000 please ) function LeapYear(year){ if ((year/4) != Math.floor(year/4)) return false; if ((year/100) != Math.floor(year/100)) return true; if ((year/400) != Math.floor(year/400)) return false; return true; } // email checker, mus be of format x@x.x function isEmail(s){ // is it blank or any white space chars?? if (isWhitespace(s)) return true; // there must be >= 1 character before @, so we // start looking at character position 1 // (i.e. second character) var i = 1; var sLength = s.length; // look for @ while ((i < sLength) && (s.charAt(i) != "@")) { i++ } if ((i >= sLength) || (s.charAt(i) != "@")) return true; else i += 2; // look for . while ((i < sLength) && (s.charAt(i) != ".")) { i++ } // there must be at least one character after the . if ((i >= sLength - 1) || (s.charAt(i) != ".")) return true; else return false; } //******************************************************* // Form element checks //******************************************************* // Check selection box has received a selection function isSelected(o) { if (o.selectedIndex == 0) return true; return false; // one was selected } //******************************************************* // Process forms //******************************************************* // Display the errors that occured function error(s){ alert("The following errors occurred:\n" +"------------------------------------------------------\n" +s +"------------------------------------------------------\n" +"Please check your entries" ); return false; } //******************************************************* // The end //*******************************************************