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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    reformatting phone number

    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.
    Last edited by hello1215; 08-26-2002 at 06:01 PM.

  • #2
    New Coder
    Join Date
    Jul 2002
    Location
    Alcoy (Spain)
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    substring

    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.

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Alcoy (Spain)
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile sorry!!

    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);

  • #4
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So far, this is what I have

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


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

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

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    function formatPhone(phone) {
    return phone.toString().replace(/\D/g,'').replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
    }

    something like that should work.


  •  

    Posting Permissions

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