Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Phone number format using Java Script

Phone number format using Java Script

Scheduled Pinned Locked Moved ASP.NET
javatoolshelptutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aslesh
    wrote on last edited by
    #1

    Hi, I have a requirement where i need to format the phone number in 111-111-1111 format after it loses focus. Can any one help me how to do this? Santhapur

    Steve EcholsS 1 Reply Last reply
    0
    • A Aslesh

      Hi, I have a requirement where i need to format the phone number in 111-111-1111 format after it loses focus. Can any one help me how to do this? Santhapur

      Steve EcholsS Offline
      Steve EcholsS Offline
      Steve Echols
      wrote on last edited by
      #2

      I was curious, so whipped up a test page: function formatValue() { var v = document.getElementById('val').value; var fv = document.getElementById('fval'); var re = /^(\d{3})[-]?(\d{3})[-]?(\d{4})$/ if(re.test(v) == true) { fv.innerHTML = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3; } else { fv.innerHTML = "Bad!"; } }

      The magic is in the regex. You can tweak it to accept other delimiters other than "-". - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

      • S
        50 cups of coffee and you know it's on!
        Code, follow, or get out of the way.
      A 1 Reply Last reply
      0
      • Steve EcholsS Steve Echols

        I was curious, so whipped up a test page: function formatValue() { var v = document.getElementById('val').value; var fv = document.getElementById('fval'); var re = /^(\d{3})[-]?(\d{3})[-]?(\d{4})$/ if(re.test(v) == true) { fv.innerHTML = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3; } else { fv.innerHTML = "Bad!"; } }

        The magic is in the regex. You can tweak it to accept other delimiters other than "-". - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

        A Offline
        A Offline
        Aslesh
        wrote on last edited by
        #3

        Hey Thanks Its working but.. But i need to display the formatted output again in the textbox itself. How can we do it ? Please help me i am a begineer in the javascript programming. Or else give me a small clue i can try it out.. Santhapur

        Steve EcholsS 1 Reply Last reply
        0
        • A Aslesh

          Hey Thanks Its working but.. But i need to display the formatted output again in the textbox itself. How can we do it ? Please help me i am a begineer in the javascript programming. Or else give me a small clue i can try it out.. Santhapur

          Steve EcholsS Offline
          Steve EcholsS Offline
          Steve Echols
          wrote on last edited by
          #4

          In my sample, replace: fval.innerHTML = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3; with: document.getElementById('val').value = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3; You can work out the best way of handling bad input however you want, maybe an alert or an error message.


          - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

          • S
            50 cups of coffee and you know it's on!
            Code, follow, or get out of the way.
          A 1 Reply Last reply
          0
          • Steve EcholsS Steve Echols

            In my sample, replace: fval.innerHTML = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3; with: document.getElementById('val').value = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3; You can work out the best way of handling bad input however you want, maybe an alert or an error message.


            - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

            A Offline
            A Offline
            Aslesh
            wrote on last edited by
            #5

            Hey, Thanks I did it by own and anyways Thanka a lot for your help Santhapur

            Steve EcholsS P 2 Replies Last reply
            0
            • A Aslesh

              Hey, Thanks I did it by own and anyways Thanka a lot for your help Santhapur

              Steve EcholsS Offline
              Steve EcholsS Offline
              Steve Echols
              wrote on last edited by
              #6

              Glad to help, and I learned something new today. :)


              - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

              • S
                50 cups of coffee and you know it's on!
                Code, follow, or get out of the way.
              1 Reply Last reply
              0
              • A Aslesh

                Hey, Thanks I did it by own and anyways Thanka a lot for your help Santhapur

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #7

                There are several good articles on this site in regards to regular expressions and the Expresso tool for building regular expressions. I suggest you check them out during your free time :)

                "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups