Skip to content

Instantly share code, notes, and snippets.

@computercarguy
Created June 26, 2012 17:36
Show Gist options
  • Save computercarguy/2997308 to your computer and use it in GitHub Desktop.
Save computercarguy/2997308 to your computer and use it in GitHub Desktop.
JavaScript phone number validation
function FormatPhone(ThePhone){
if (ThePhone.length >= 9){
ThePhone = ThePhone.replace(/\(/gi,"");
ThePhone = ThePhone.replace(/\)/gi,"");
ThePhone = ThePhone.replace(/-/gi,"");
ThePhone = ThePhone.replace(/\ /gi,"");
return ThePhone;
} else {
return "invalid";
} }
function NoChrs(Field){
ChangedPage = true;
AllowedChrs = "";
if (Field == "Phone"){
AllowedChrs = "().-";
}
tempArr = document.getElementById(Field).value.split("");
for (i=0;i<tempArr.length;i++){
if ((isNaN(parseFloat(tempArr[i]))) && (AllowedChrs.indexOf(tempArr[i]) == -1)){
tempArr[i] = "";
} }
document.getElementById(Field).value = tempArr.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment