View Full Version : reformatting phone nubmer

08-26-2002, 04:52 PM
How can I reformat phone numbers to contain dashes,
ex: 123-456-7890. I want the dashes to be added once the user leaves the field.

08-26-2002, 05:06 PM
I imagine tha all the phone numbers in your country have the same length. If so you can do this:

imagine you have number here: form1.phone.value

number = form1.phone.value;
new = number.subtring(0,3)+"-"+number.subtring(4,6)+"-"+number.subtring(7,9);

I think that's it. You have cut the phone number in three parts with dashes inside.

08-26-2002, 05:07 PM
What a mess!!!
I've forgotten the "s" of substring. Try with this one.

new = number.substring(0,3)+"-"+number.substring(4,6)+"-"+number.substring(7,9);

08-26-2002, 05:36 PM
So far, this is what I have

<script language="JavaScript">
function number(field)
return (reformat(field , "",3, "-",3, "-",4))

<form name="form" >
<td><input type="text" name="phone" value="" maxlength=12 onChange="number(this)"></td>

What do I need to do to get this to work? Adding dashes once the user leaves the field.

08-26-2002, 07:59 PM
function formatPhone(phone) {
return phone.toString().replace(/\D/g,'').replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');

something like that should work.