Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating telephone numbers

Status
Not open for further replies.

meeble

Programmer
Sep 24, 2002
137
GB
I use this function to validate the telephone number in a form box. How do I amend it so it allows spaces as well as valid numbers?

Cheers

James

function checknumber(){
var x=document.myFormName.Telephone_Home.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("Please input a valid number!")
testresult=false
}
return (testresult)
}
 
spaces where? space only? or something like

"4 5 5 4 1 1 1 222 2"?

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
can u give us the format that u want???

Known is handfull, Unknown is worldfull
 
Personally, this is the one I use for phone validation, it's been working so far.

Code:
/^(\(\d{3}\)|\d{3})\s*\d{3}\s*-?\s*\d{4}$/

with or without parenthesis, with or without spaces, with or without the dash.

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
I wrote this script a while back, it isn't the most efficient probably, JS isn't my strong suite.

<script language = "javascript">
function formatPhoneNumber(fld){
if (window.event.keyCode != 8){ //Ignore formatting if user is backspacing.
if (fld.value.charAt(fld.value.length-1) != "("){ //Allow the first instance of the open peren.
lastChar = fld.value.charAt(fld.value.length-1).replace(/\D*/, ''); //set a variable to the value of the last character in the string after replacing all non numeric chars.
if (lastChar == ""){ //remove the invalid character
fld.value = fld.value.substring(0, fld.value.length-1); //set the field to the correct data
}
}
if(fld.value.length == 1 && fld.value.charAt(fld.value.length-1) != "("){ //check to see if user already added the opening peren.
fld.value = "(" + fld.value; //If they didn't, add it for them.
}
if(fld.value.length == 4){ //Check to see if the area code is complete
fld.value = fld.value + ")"; //Add the closing peren after the area code.
}
if(fld.value.length == 8){ //Check to see if the prefix has been entered.
fld.value = fld.value + "-"; //If the prefix has been entered, insert the dash.
}
}
}
</script>

<form name = "phoneNumber">
<input type = "text" name = "phoneNumberToFormat" onkeyup = "formatPhoneNumber(this);">
</form>

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top