View Full Version : Formatting a phone number for output ...

03-14-2004, 10:28 PM
I have an array from a drop down list that passes all the values that I am then redisplaying in text boxes below the drop-down. The phone number is in many different formats, but I am trying to standardize the output on screen. For example, if the country is "United States", I break the array element into 3 pieces to format the phone into 3 different input boxes liek so: [555][555][5555]. My problem is the phone numbers are not always just numbers. Some are formatted like this: (555) 555-5555. So, I want to convert my array into a string and replace anything that is not a number 0-9. Here is what I have, but it's not working.

if (vArray[2] == 'United States') {
var myPhoneString = String(vArray[7])
myPhoneString.replace(/^0-9/g, '');
eval("document.frmShip."+pType+"Phone1D.value = myPhoneString.substring(0,3)");
eval("document.frmShip."+pType+"Phone2D.value = myPhoneString.substring(3,5)");
eval("document.frmShip."+pType+"Phone3D.value = myPhoneString.substring(6,9)");

03-14-2004, 11:07 PM
This should fit into your setup:

myPhoneString=myPhoneString.replace(/\D/g, '');

03-14-2004, 11:22 PM
Worked great ... thanks!

03-15-2004, 04:41 AM
...And you should avoid using eval.

document.frmShip.elements[pType+"Phone1D"].value = myPhoneString.substring(0,3);