form_fields = []; // field types and identifiers used in form_fields matrix required_field = 0; required_ifvalue = 1; required_ifnotvalue = 2; required_twin = 3; required_radio = 4; checkboxgroup_min = 5; checkboxgroup_max = 6; fieldgroup_required = 7; fieldgroup_optional = 8; text_field = 9; normal_text = 10; numeric_text = 11; alphabetic_text = 12; alphanumeric_text = 13; no_space = false; allow_space = true; name_field = 14; text_minlength = 15; textarea_maxlength = 16; text_maxwords = 17; min_value = 18; max_value = 19; decimal_field = 20; date_field = 21; any_date = false; future_date = true; deadline_date = 22; email_field = 23; usa_phone_field = 24; formatted_field = 25; format_ifvalue = 26; format_ifnotvalue = 27; lte = 28; // less and equal than lt = 29; // less than gte = 30; // bigger and equal than gt = 31; // bigger than required_ifcheck = 32; required_ifnotcheck = 33; credit_card = 34; credit_card_mask = 35; checkboxgroup_min_ifvalue = 36; checkboxgroup_max_ifvalue = 37; phone_field = 38; ssn_field = 39; function validate_syntax(form_name) // returns true if all data entered in form is syntactically correct { // if form_fields has not been populated, then // assume form is valid (no validation specified) if(form_fields.length == 0) return true; // obtain reference to form form_object = document.forms[form_name]; // assume syntax is valid syntax_valid = true; // check fields for(i = 0 ; i < form_fields.length && syntax_valid ; i++) { status = "Validating form... " + Math.floor((i / form_fields.length) * 100) + "%"; field_num = i; field_type = form_fields[i][1][0]; field_name = form_fields[i][0]; var loc = window.location.toString(); var langin = loc.indexOf("ln=tr"); if(field_type == required_field) syntax_valid = get_field_value(form_object,field_name); else if(field_type == required_ifvalue) { field_value = get_field_value(form_object,field_name); test_field = form_fields[i][1][1]; test_values = form_fields[i][1][2]; syntax_valid = true; for(j = 0 ; j < test_values.length && syntax_valid ; j++) if(!field_value && (get_field_value(form_object,test_field) == test_values[j])) syntax_valid = false; } else if(field_type == required_ifnotvalue) { field_value = get_field_value(form_object,field_name); test_field = form_fields[i][1][1]; test_values = form_fields[i][1][2]; field_valuecount = 0; for(j = 0 ; j < test_values.length ; j++) if(get_field_value(form_object,test_field) != test_values[j]) field_valuecount++; if(!field_value && (field_valuecount == test_values.length)) syntax_valid = false; else syntax_valid = true; } else if(field_type == checkboxgroup_min_ifvalue || field_type == checkboxgroup_max_ifvalue ) { field_nameprefix = field_name; minmax_num = form_fields[i][1][1]; index_offset = get_field_index(form_object,field_nameprefix); num_fields = form_fields[i][1][2]; field_valuecount = 0; for(j = 0 ; j < num_fields ; j++) if(form_object[j + index_offset].checked) field_valuecount++; test_field = form_fields[i][1][3]; test_values = form_fields[i][1][4]; syntax_valid = true; for(j = 0 ; j < test_values.length && syntax_valid ; j++) if(get_field_value(form_object,test_field) == test_values[j]) { if(field_type == checkboxgroup_min_ifvalue) syntax_valid = (field_valuecount >= minmax_num); else if(field_type == checkboxgroup_max_ifvalue) syntax_valid = (field_valuecount <= minmax_num); } } else if(field_type == required_twin) { field_value = get_field_value(form_object,field_name); twin_name = form_fields[i][1][1]; twin_value = form_object[twin_name].value; syntax_valid = (field_value == twin_value); } else if(field_type == required_radio) { field_value = get_field_value(form_object,field_name); syntax_valid = (field_value != null); } else if(field_type == checkboxgroup_min || field_type == checkboxgroup_max) { field_nameprefix = field_name; minmax_num = form_fields[i][1][1]; index_offset = get_field_index(form_object,field_nameprefix); num_fields = form_fields[i][1][2]; field_valuecount = 0; for(j = 0 ; j < num_fields ; j++) if(form_object[j + index_offset].checked) field_valuecount++; if(field_type == checkboxgroup_min) syntax_valid = (field_valuecount >= minmax_num); else if(field_type == checkboxgroup_max) syntax_valid = (field_valuecount <= minmax_num); } else if((field_type == fieldgroup_required) || (field_type == fieldgroup_optional)) { field_nameprefix = field_name; field_names = form_fields[i][1][1]; num_fields = field_names.length; field_valuecount = 0; for(j = 0 ; j = min_length)); } else if(field_type == textarea_maxlength) { field_len = form_object[field_name].value.length; max_length = form_fields[i][1][1]; syntax_valid = ((field_len == 0) || (field_len <= max_length)); } else if(field_type == text_maxwords) { field_value = form_object[field_name].value; delimiter = form_fields[i][1][1]; max_count = form_fields[i][1][2]; syntax_valid = check_delimited(field_value,delimiter,max_count); } else if(field_type == min_value || field_type == max_value) { field_value = get_field_value(form_object,field_name); min_max = form_fields[i][1][1]; if(field_type == min_value) syntax_valid = (field_value >= min_max); else if(field_type == max_value) syntax_valid = (field_value <= min_max); } else if(field_type == decimal_field) { decimal = form_object[field_name].value; if(decimal) { field_value = ""; for(k = 0 ; k < decimal.length ; k++) if(decimal.charAt(k) != '$' && decimal.charAt(k) != ',') field_value += decimal.charAt(k); form_object[field_name].value = field_value; decimal_places = form_fields[i][1][1]; syntax_valid = check_decimal(field_value,decimal_places); } } else if(field_type == date_field) { month_value = parseInt(form_object[field_name + "month"].value,10); day_value = parseInt(form_object[field_name + "day"].value,10); year_value = parseInt(form_object[field_name + "year"].value,10); hour_field = form_object[field_name + "hour"]; hour_value = parseInt(hourfield.options[hour_field.selectedIndex].value,10); future_only = form_fields[i][1][1]; error_string = check_date(month_value,day_value,year_value,hour_value,future_only); form_fields[field_num][2] = error_string; syntax_valid = (error_string == null); } else if(field_type == email_field) { field_value = form_object[field_name].value; syntax_valid = (!field_value || check_email(field_value)); } else if(field_type == usa_phone_field) { field_value = form_object[field_name].value; syntax_valid = (!field_value || check_usa_phone(field_value)); } else if(field_type == formatted_field) { field_value = form_object[field_name].value; if(field_value) { format_strings = form_fields[i][1][1]; syntax_valid = check_format_all(field_value,format_strings); } } else if(field_type == format_ifvalue) { field_value = get_field_value(form_object,field_name); if(field_value) { test_field = form_fields[i][1][1]; test_values = form_fields[i][1][2]; format_needed = false; for(j = 0 ; j < test_values.length && !format_needed ; j++) if(get_field_value(form_object,test_field) == test_values[j]) format_needed = true; if(format_needed) { format_strings = form_fields[i][1][3]; syntax_valid = check_format_all(field_value,format_strings); } else syntax_valid = true; } else syntax_valid = true; } else if(field_type == format_ifnotvalue) { field_value = get_field_value(form_object,field_name); if(field_value) { test_field = form_fields[i][1][1]; test_values = form_fields[i][1][2]; field_valuecount = 0; for(j = 0 ; j < test_values.length ; j++) if(get_field_value(form_object,test_field) != test_values[j]) field_valuecount++; format_needed = (field_valuecount == test_values.length); if(format_needed) { format_strings = form_fields[i][1][3]; syntax_valid = check_format_all(field_value,format_strings); } else syntax_valid = true; } else syntax_valid = true; } else if(field_type == lte) { field_value = get_field_value(form_object,field_name); name_arr = form_fields[i][1][1]; for (j=0; j parseFloat(get_field_value(form_object,name_arr[j])))) {syntax_valid = false; break; } } else if(field_type == lt) { field_value = get_field_value(form_object,field_name); name_arr = form_fields[i][1][1]; for (j=0; j= parseFloat(get_field_value(form_object,name_arr[j])))) {syntax_valid = false; break; } } else if(field_type == gte) { field_value = get_field_value(form_object,field_name); name_arr = form_fields[i][1][1]; for (j=0; j' , '?' , '/' , '\r' , '\n' ]; function is_valid_character(test_char) // returns true if character is in valid_chars array { for(j = 0 ; j < valid_chars.length ; j++) if(test_char == valid_chars[j]) return true; return false; } function check_text(text_string,spaces_allowed) // returns true if text_string only contains valid characters // (a-z, A-Z, 0-9, and characters in valid_chars) { text_valid = true; for(k = 0 ; k < text_string.length && text_valid ; k++) { test_char = text_string.charAt(k); text_valid &= (test_char >='a' && test_char <='z') || (test_char >= 'A' && test_char <= 'Z') || (test_char >= '0' && test_char <= '9') || is_valid_character(test_char) || (spaces_allowed && (test_char == ' ')); } return text_valid; } function check_delimited(text_string,delimiter,max_count) { phrase_count = (text_string.split(delimiter).length <= max_count); return phrase_count; } function check_numeric(numeric_string,spaces_allowed) // returns true if numeric_string only contains numbers { numeric_valid = true; for(k = 0 ; k < numeric_string.length && numeric_valid ; k++) { test_char = numeric_string.charAt(k); numeric_valid &= (test_char >= '0' && test_char <= '9') || (spaces_allowed && (test_char == ' ')); } return numeric_valid; } function check_alphabetic(alphabetic_string,spaces_allowed) // returns true if alphabetic_string only contains letters { alphabetic_valid = true; for(k = 0 ; k < alphabetic_string.length && alphabetic_valid ; k++) { test_char = alphabetic_string.charAt(k); alphabetic_valid &= (test_char >= 'a' && test_char <= 'z') || (test_char >= 'A' && test_char <= 'Z') || (spaces_allowed && (test_char == ' ')); } return alphabetic_valid; } function check_alphanumeric(alphanumeric_string,spaces_allowed) // return true if alphanumeric_string only contains letters and numbers { alphanumeric_valid = true; for(k = 0 ; k < alphanumeric_string.length && alphanumeric_valid ; k++) { test_char = alphanumeric_string.charAt(k); alphanumeric_valid &= (test_char >= 'a' && test_char <= 'z') || (test_char >= 'A' && test_char <= 'Z') || (test_char >= '0' && test_char <= '9') || (spaces_allowed && (test_char == ' ')); } return alphanumeric_valid; } function check_name(name_string) // return true if name_string only contains letters, ' , - , and spaces { name_valid = true; for(k = 0 ; k < name_string.length && name_valid ; k++) { test_char = name_string.charAt(k); name_valid &= (test_char >= 'a' && test_char <= 'z') || (test_char >= 'A' && test_char <= 'Z') || (test_char == '\'') || (test_char == '-') || (test_char == ' '); } return name_valid; } function check_decimal(decimal_string,decimal_places) // returns true if number_string has no decimal, or if number // of digits in decimal is less than or equal to decimal_places { if(decimal_string.charAt(0) == '.') return false; decimal_len = decimal_string.length; if(decimal_string.indexOf('.') != -1) { decimal = decimal_len - decimal_string.lastIndexOf('.') - 1; decimal_valid = (decimal != 0) && (decimal <= decimal_places ) && check_numeric(decimal_string.substring(0,decimal_len - decimal - 1),no_space) && check_numeric(decimal_string.substring(decimal_len - decimal),no_space); } else decimal_valid = check_numeric(decimal_string,no_space); return decimal_valid; } function check_date(month,day,year,hour,future_only) { if(month == 0) return "Month of must be a number within 1 and 12."; if(day == 0) return "Day may not be zero."; if(year == 0) return "Year must be a non-zero four-digit number."; months = ["January","February","March","April","May","June", "July","August","September","October","November","December"]; numdays = [31,0,31,30,31,30,31,31,30,31,30,31]; if(year % 4 == 0) numdays[1] = 29; else numdays[1] = 28; month_index = month - 1; if(day > numdays[month_index]) return "There are " + numdays[month_index] + " days in " + months[month_index] + ", " + year + "."; if(future_only) { m = month - 1; t = new Date(); m1 = t.getMonth(); d1 = t.getDate(); y1 = t.getFullYear(); h1 = t.getHours(); if(d1 == numdays[m1] && h1 >= (endhour - beginhour_offset)) { d1 = 1; m1++; if(m1 > 11) { m1 = 1; y1++; } } if(year < y1) return "Year must be " + y1 + " or later."; if(m < m1 && year == y1) return "Month must be " + months[m1] + " or later."; one = d1 % 10; ten = Math.floor(d1 / 10); if(ten != 1 && one == 1) suffix = "st"; else if(ten != 1 && one == 2) suffix = "nd"; else if(ten != 1 && one == 3) suffix = "rd"; else suffix = "th"; if(day < d1 && m == m1 && year == y1) return "Day must be the " + d1 + suffix + " or later."; } if(deadline_month != null) { if(((month - 1) == deadline_month && day > deadline_day && year == deadline_year) || ((month - 1) > deadline_month && year >= deadline_year)) return "Please enter a date before the deadline."; } return null; } function check_email(email_string) // returns true if email_string is in proper // mailbox@domain.suffix or mailbox@[n.n.n.n] format { email_halves = email_string.split('@') if(email_halves.length != 2) return false; email_name = email_halves[0]; email_domain = email_halves[1]; if((email_domain.charAt(0) == "[") || (email_domain.charAt(email_domain.length - 1) == "]")) { if((email_domain.charAt(0) == "[") && (email_domain.charAt(email_domain.length - 1) == "]")) { ip_octets = (email_domain.substring(1,email_domain.length - 1)).split('.'); if(ip_octets.length != 4) return false; for(k = 0 ; k < ip_octets.length ; k++) if(ip_octets[k].length == 0 || ip_octets[k].length > 3 || !check_numeric(ip_octets[k],false)) return false; } else return false; } else { domain_parts = (email_domain).split('.'); domain_suffix = domain_parts[domain_parts.length - 1]; if(!check_alphabetic(domain_suffix,no_space)) return false; suffix_len = domain_suffix.length; if(suffix_len < 2 || suffix_len > 3) return false; for(k = 0 ; k < domain_parts.length ; k++) if(domain_parts[k].length == 0) return false; } name_parts = (email_name).split('.'); for(k = 0 ; k < name_parts.length ; k++) if(name_parts[k].length == 0) return false; return true; } function check_usa_phone(phone_string) // returns true if phone_string is in one of these formats: // 000-000-0000, (000)000-0000, or 1-000-000-0000 { phone_valid = check_format(phone_string,"nnn-nnn-nnnn") || check_format(phone_string,"(nnn)nnn-nnnn") || check_format(phone_string,"n-nnn-nnn-nnnn"); return phone_valid; } function check_format(input_string,format_string) // return true if input_string matches format specified in format_string // .. input_string must have alphabetic character wherever 'a' appears in format_string // .. input_string must have numeric character wherever 'n' appears in format_string // .. all other non-alphanumeric characters must match exactly // for example: // .. the format_string for a date could be: "nn/nn/nnnn" // .. the format_string for a license plate could be: "aaa nna" // .. the format_string for a social security number could be: "nnn-nn-nnnn" { test_string = "" + input_string; if(test_string.length != format_string.length) return false; format_valid = true; for(c = 0 ; c < format_string.length ; c++) { format_char = format_string.charAt(c); test_char = test_string.charAt(c); if((format_char == 'n') && !(test_char >= '0' && test_char <= '9')) { format_valid = false; break; } else if((format_char == 'a') && !(test_char >= 'a' && test_char <= 'z') && !(test_char >= 'A' && test_char <= 'Z')) { format_valid = false; break; } else if((format_char != 'a') && (format_char !='n') && (format_char != test_char)) { format_valid = false; break; } } return format_valid; } function check_format_all(input_string,format_strings) // return true if check_format passes for all formats in array of format_strings { format_test = false; for(k = 0 ; k < format_strings.length && !format_test ; k++) format_test |= check_format(field_value,format_strings[k]); return format_test; } function check_credit_card(card_number, card_type) { // return true if the credit card format is correct according to the card type if ( card_type == "AMEX" ) card_pattern = /^[0-9]{4}[- ]?[0-9]{6}[- ]?[0-9]{5}$/; else card_pattern = /^[0-9]{4}[- ]?[0-9]{4}[- ]?[0-9]{4}[- ]?[0-9]{4}$/; return card_pattern.test(card_number); } function check_mask_credit_card(card_number, card_type) { // return true if the credit card mask format is correct according to the card type // all the begining number are asterisk and last 4 digits if ( card_type == "AMEX" ) mask_pattern = /^(\*){4}[- ]?(\*){6}[- ]?(\*)[0-9]{4}$/; else mask_pattern = /^(\*){4}[- ]?(\*){4}[- ]?(\*){4}[- ]?[0-9]{4}$/; return mask_pattern.test(card_number); } function check_phone_number(Tel_Num) { var SpecChar="-().+ "; var VerifyChar; var aChar = 0; for(var i=0; i < Tel_Num.length; i++) { aChar= (Tel_Num).charAt(i); VerifyChar=SpecChar.indexOf(aChar) if(VerifyChar==-1 && !(aChar >= "0" && aChar <= "9")) { return false; } } return true; } function SSNValidation(ssn) { var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/); var numDashes = ssn.split('-').length - 1; if (matchArr == null || numDashes == 1) { alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.'); msg = "does not appear to be valid"; return false; } else if (parseInt(matchArr[1],10)==0) { alert("Invalid SSN: SSN's can't start with 000."); msg = "does not appear to be valid"; return false; } }