function validatePhone( vField, vEdits ) { //strip input of spaces, hyphens, underscores, parenthesis, and slashes to prepare for validation testing var ph_str = strip_chars(vField.value, " -_()/ \\"); var ph_len = ph_str.length; var phonetype = "Both"; var errorstatusUS = "False"; var errorstatusIN = "False"; var msg = ""; var msgbase = "Please enter a valid Phone Number with area code. \nIt can only contain numbers, dashes, parenthesis.\n\n"; var msgUS = "Do not include '1' for long distance.\nInternational phone numbers are not valid for this field."; var msgIN = "It must begin with '011'.\nUS phone numbers are not valid for this field."; var msgBoth = "Do not include '0' or '1' for long distance.\nInternational phone numbers must begin with '011'."; //test if there is no input (not required), get other edit flags. if (vEdits != null) { if ( (vEdits.indexOf("nr") > -1) || (vEdits.indexOf("NR") > -1) ) { if (ph_str == ""){ vField.value = ""; return true; } } if ( (vEdits.indexOf("us") > -1) || (vEdits.indexOf("US") > -1) ) { phonetype = "US"; msg = msgbase + msgUS; } if ( (vEdits.indexOf("in") > -1) || (vEdits.indexOf("IN") > -1) ) { phonetype = "IN"; msg = msgbase + msgIN; } } if ( msg == "" ) { msg = msgbase + msgBoth; } //Check if valid US phone number //test for leading zero or 1 if ((ph_str.substring(0,1) == 0) || (ph_str.substring(0,1) == 1)) { errorstatusUS = "True"; } //test the phone number has 10 digits if (ph_len != 10) { errorstatusUS = "True"; } //Check if valid International phone number //test for leading 011 if (ph_str.substring(0,3) != "011") { errorstatusIN = "True"; } //test the phone number has 6 to 17 digits if ( (ph_len < 6) || (ph_len > 17) ) { errorstatusIN = "True"; } // test for numerics (ch is assigned a numeric value) //loop: as long as j equals numerics, add one to j and test the next value till you reach // the length of the string for (var j = 0; j < ph_len; j++) { var ch = ph_str.substring(j, j+1); if ((ch < "0") || ( ch > "9")) { errorstatusUS = "True"; errorstatusIN = "True"; } } // Check flags to see if phone number is valid and of the proper type. if ( ( errorstatusUS == "True" ) && ( errorstatusIN == "True") ) { alertBox( vField, msg, "text"); return false; } if ( ( phonetype == "US") && ( errorstatusUS == "True") ) { alertBox( vField, msg, "text"); return false; } if ( ( phonetype == "IN") && ( errorstatusIN == "True") ) { alertBox( vField, msg, "text"); return false; } // Phone number is valid, so reformat according to type. // Since the International and US formats are mutually exclusive (US doesn't allow leading 0, and International requires leading 0), // we only need to check to see which format did not receive an error. //reformat US phone number (area code) 123-4567 if (errorstatusUS == "False" ) { vField.value = "("+ ph_str.substring(0,3) + ") " + ph_str.substring(3,6) + "-" + ph_str.substring(6,10); } //reformat International phone number 011-12345678901234 if (errorstatusIN == "False" ) { vField.value = ph_str.substring(0,3) + "-" + ph_str.substring(3, ph_len); } return true; } function ValidatePhone( vField, vEdits ) { return validatePhone( vField, vEdits ) }