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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Phone #

Status
Not open for further replies.

LOW

Programmer
Sep 27, 2000
45
0
0
US
I would like to force a certain phone format xxx-xxx-xxxx in a form text field. Does anyone have an idea how to do that? Thank you.
 
Here is a rough Reg Ex example:
Code:
phoneNo="222 222-2222"
if (phoneNo.match(/^\d\d\d\-\d\d\d-\d\d\d\d$/)) alert("its good")
else alert("no good")
Where phoneNo will probably be equal to the field that holds the phone number.

HTH,
Matt
 
Thank you. Would onChange be the preferred event handler for this? I'm a novice, so please forgive the dumb questions.
 
Is the phone number the only thing you're checking?

I would check the entire form once the submit button has been clicked...
Code:
<script>
function checkForm() {
//check for phone number here...
phoneNo=document.getElementById("phone").value
if (phoneNo.match(/^\d\d\d\-\d\d\d-\d\d\d\d$/)) return true;
else {
alert("Please check your phone number")
return false
}
}
</script>
<form method="get" action="url" onsubmit="return checkForm()">
<input type="text" id="phone">
...
<input type="submit">
</form>
I just typed that up off the top of my head. So, let me know if it breaks or if you need some more help.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top