Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Formatting a phone number for output ...

    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.

    Code:
    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)");
    			}

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    This should fit into your setup:

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

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Worked great ... thanks!

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    ...And you should avoid using eval.

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

    http://www.litotes.demon.co.uk/js_info/sq_brackets.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •