// Validation Code - Mark Vovsi - 05/30/2001 // Verification function. Checks to see if fields with the "required" option marked are blank. // for now, aimed only at text and textarea fields function verify( form ) { // variables var msg; var emptyFields = ""; var dateErrors = ""; firstFailField = ""; //var dateOK = false; // Loop through the elements of the form, looking for all elements that // have a "required" property defined. Then, check if these fields are // empty and make a list of them. Put together error messages. Then // check for "date fields" and check for correct pattern. for ( var i = 0; i < form.length; i++ ) { var element = form.elements[ i ]; if ( element.required ) { // check if the field is empty, and flag the firstFailField, to know where // to focus() later on. if ( noData( element.value) ) { emptyFields += "\n " + element.label; if ( firstFailField == "" ) firstFailField = element; continue; // if field is blank, don't bother checking date } } // if the element is NOT required, if it's blank don't bother checking the date // and don't bother producing error messages if ( element.notRequired ) if ( noData( element.value ) ) continue; if ( element.isEmail ) { if ( !validEmail( element.value ) ) { emptyFields += "\n " + element.label; if ( firstFailField == "" ) firstFailField = element; continue; // since this is an email field, it's definitely not a date, so don't bother checking for date errors } } if ( element.isDateField ) { // in case it's a field with multiple dates, i.e. 01/02/2002, 12/23/2004 // let's break the string into an array of individual dates // and make sure that blank spaces are trimmed var dateArray = trimArrayData( element.value.split( "," ) ); var dateCheckResult = isValidDate( dateArray, element.label ); if ( dateCheckResult != true ) { dateErrors += dateCheckResult; dateErrors += ".\n"; if ( firstFailField == "" ) firstFailField = element; } } } // Now, if there were any errors, display the messages, and return // false to prevent the form from being submitted. if ( emptyFields || dateErrors ) { msg = "_________________________________________________ \n\n"; msg += "The form was not submitted because of the following error(s).\n"; msg += "Please correct the errors and re-submit.\n"; msg += "_________________________________________________ \n\n"; if ( emptyFields ) { msg += " - The following required field(s) are empty or invalid:" + emptyFields + "\n"; if ( dateErrors ) msg += "\n"; } msg += dateErrors; alert( msg ); firstFailField.focus(); return false; } // else we're good return true; } // function noData(s) checks if the field is blank or has whitespace characters function noData ( field ) { // possible whitespace characters var whitespace = " \t\n\r"; // if field is blank if ( (field == "") || (field == null) ) return true; // if we find a non-whitespace char, we're ok for ( var i = 0; i < field.length; i++) { if ( whitespace.indexOf( field.charAt(i) ) == -1 ) return false; } // else all characters are whitespace return true; } // Email address must be of form a@b.c -- in other words: // * there must be at least one character before the @ // * there must be at least one character before and after the . // * the characters @ and . are both required function validEmail ( email ) { var invalidChars = " /:';"; // if there's no data in the field if ( noData( email ) ) return false; // if we find an invalid char in e-mail for ( var i = 0; i < email.length; i++ ) if ( invalidChars.indexOf( email.charAt(i) ) != -1 ) return false; // @ char checking // @ can't be the first character if ( email.charAt(0) == "@" ) return false; // check for existence of @ starting w/2nd char in email // in other words, there must be one char before @ var atPos = email.indexOf("@",1); if ( atPos == -1 ) return false; // make sure there's only one @ // and that there's chars after it if ( email.indexOf("@",atPos+1) != -1 ) return false; // period checking // check that there's no period right after @ if ( email.charAt(atPos+1) == "." ) return false; // check that there's a period after @, considering //there's some legal char b/t @ and the period var periodPos = email.indexOf(".",atPos+2); if ( periodPos == -1 ) return false; // make sure there are at least 2 chars after the period if ( periodPos+3 > email.length ) return false; // else we're cool return true; } // isValidDate() - checks for the proper date entry pattern. // Returns a String error message, or if data is valid, returns true. // Checks for the following valid date formats: // MM/DD/YY MM/DD/YYYY // Also separates date into month, day, and year variables // var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{2}|\d{4})$/; // To require a 4 digit year entry, use this line instead: // var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/; // Function is optimized to check an array of date strings function isValidDate( dateStrArray, fieldName ) { var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/; for ( i = 0; i < dateStrArray.length; i++ ) { // check format var matchArray = dateStrArray[i].match(datePat); if (matchArray == null) { return ( "- " + fieldName + " is not in a valid format"); } // parse date into variables month = matchArray[1]; day = matchArray[3]; year = matchArray[4]; // check month range if (month < 1 || month > 12) { return ( "- " + fieldName + " - month must be between 1 and 12" ); } // check days range if (day < 1 || day > 31) { return ( "- " + fieldName + " - day must be between 1 and 31" ); } // check day count for months w/only 30 days if ((month==4 || month==6 || month==9 || month==11) && day==31) { return ( "- " + fieldName + " - month " + month + " doesn't have 31 days"); } // check for February 29th if ( month == 2 ) { var isleap = ( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) ); if ( day > 29 || ( day==29 && !isleap ) ) { return ( "- " + fieldName + " February " + year + " doesn't have " + day + " days" ); } } } // else we're good ... date is valid return true; } // String utility functions function trim( str ) { var resultStr = ""; resultStr = trimLeft( str ); resultStr = trimRight( resultStr ); return resultStr; } function trimLeft( str ) { var resultStr = ""; var i = 0; // Return immediately if an invalid value was passed in if (str+"" == "undefined" || str == null) return null; // Make sure the argument is a string str += ""; if ( str.length == 0 ) resultStr = ""; else { // Loop through string starting at the beginning as long as there are spaces. while ( ( i <= str.length ) && ( str.charAt(i) == " " ) ) i++; // When the loop is done, we're sitting at the first non-space char, // so return that char plus the remaining chars of the string. resultStr = str.substring( i, str.length ); } return resultStr; } function trimRight( str ) { var resultStr = ""; // Return immediately if an invalid value was passed in if ( str+"" == "undefined" || str == null) return null; // Make sure the argument is a string str += ""; if (str.length == 0) resultStr = ""; else { // Loop through string starting at the end as long as there are spaces. var i = str.length - 1; while ( ( i >= 0 ) && ( str.charAt(i) == " " ) ) i--; // When the loop is done, we're sitting at the last non-space char, // so return that char plus all previous chars of the string. resultStr = str.substring(0, i + 1); } return resultStr; } // trimArrayData() - makes sure that strings in an array have no blanks around data function trimArrayData( strArray ) { for ( i = 0; i < strArray.length; i++ ) strArray[ i ] = trim( strArray[ i ] ); return strArray; } // removeBlanks() - removes whitespaces from a string function removeBlanks( str ) { var a = str.split(""); for ( i=0; i < a.length; i++ ) { if ( a[ i ] == " " ) a[ i ] = ""; } return a.join(""); } // End of Mark Vovsi's Validation Code