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

add dashes auto with phone num 3

Status
Not open for further replies.

schase

Technical User
Sep 7, 2001
1,756
US
Howdy all,

Does anyone have an idea they could share with me, on how to auto add dashes as someone enters a phone number?

xxx-xxx-xxxx

thank you
"Never underestimate the power of determination"

Stuart
 
here ya go:
Code:
<html>
<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function addDashes(f)
{
num = f.phoneNum.value;
parts = [num.slice(0,3),num.slice(3,6),num.slice(6,10)];
fNum = parts[0]+&quot;-&quot;+parts[1]+&quot;-&quot;+parts[2];
alert (fNum);
}
</SCRIPT>
</head>
<BODY>
<form>
Phone Number: <input type='text' name='phoneNum'>
<BR><input type='button' onClick='addDashes(this.form)' value='Submit'>
</form>
</body>
</html>
hope it helps &quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
Hi Dookie,

Thank you for your response.

Is there a way to do it after the user has tabbed off the field?

I have three telephone fields total, home, phone, cell.

Thank you &quot;Never underestimate the power of determination&quot;

Stuart
 
onBlur event to run the function
so
onBlur=&quot;addDashes(f)&quot;
---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
getting closer.

The &quot;addDashes(f)&quot; gives me a F is undefined error.

So I changed it to

onBlur='addDashes(this.form)'

Which *works* it pops up and says what the phone number should be - but I close the alert and the phone number stays as origionally inputted - it does not get changed to include the dashes.

&quot;Never underestimate the power of determination&quot;

Stuart
 
yup, so the complete code would look like this:
Code:
<html>
<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function addDashes(f)
{
num = f.value;
parts = [num.slice(0,3),num.slice(3,6),num.slice(6,10)];
fNum = parts[0]+&quot;-&quot;+parts[1]+&quot;-&quot;+parts[2];
f.value = fNum;
}
</SCRIPT>
</head>
<BODY>
<form>
Phone: <input type='text' name='phone' onBlur='addDashes(this)'><BR>
Cell: <input type='text' name='cell' onBlur='addDashes(this)'><BR>
Home: <input type='text' name='home' onBlur='addDashes(this)'><BR>
</form>
</body>
</html>

hope it helps &quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
or if you want it really compact, heres a line:
Code:
<html>
<head>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function addDashes(f)
{
f.value = f.value.slice(0,3)+&quot;-&quot;+f.value.slice(3,6)+&quot;-&quot;+f.value.slice(6,10);
}
</SCRIPT>
</head>
<BODY>
<form>
Phone: <input type='text' name='phone' onBlur='addDashes(this)'><BR>
Cell: <input type='text' name='cell' onBlur='addDashes(this)'><BR>
Home: <input type='text' name='home' onBlur='addDashes(this)'><BR>
</form>
</body>
</html>
&quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
perfect

Thank you &quot;Never underestimate the power of determination&quot;

Stuart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top