Web Analytics Made Easy -
StatCounter 10 digit phone number validation - CodingForum

Announcement

Collapse
No announcement yet.

10 digit phone number validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 10 digit phone number validation

    How about a script that validates a 10 digit phone number? I've not found one. Using the phone number below, all parenthesis, white space and hyphens would be removed leaving only 10 digits. Thanks

    (303) 444-3303

    PS - I know I could use a 3 input model...but this way, I can pretend the form was sent if it fails validation...satisfying the pranksters. That's how I deal with it, anyway.
    Last edited by zoobie; Feb 18, 2004, 01:36 PM.
    Zoobie or not Zoobie...That is the problem.
    <body onUnload="flush( ! )">

  • #2
    Zoobie - remember that if you insist on a 10-digit phone number
    only USA residents can use the form. I have come across quite a few of these and as a result they lose a sale. Another idiocy is requiring users to select from a list of USA states, with no other option. The other day I did manage to fill out a form with the address "London England Alaska".

    Why do you want to "validate" it? The user can still get it wrong (i.e mis-typing). And surely users should not be obliged to reveal their phone number in any case? I never reveal mine. If it is obligatory in the form then I just make one up.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Well, it's only for a local biz in Denver. We have 10 digit numbers here. They are obligated to give their phone or email which I'm already validating for.
      Thanks
      Zoobie or not Zoobie...That is the problem.
      <body onUnload="flush( ! )">

      Comment


      • #4
        Zoobie,

        I'm not sure I understand what you need. Do you want to force the user to enter in the phone number in that format? If you have the form show that it was sent when it was not really sent, could you not run into possible customer service issues if a real customer makes a mistake?

        Basscyst
        Last edited by Basscyst; Feb 18, 2004, 04:24 PM.
        Helping to build a bigger box. - Adam Matthews

        Comment


        • #5
          Here is the vaidation I use in my validator script.....
          Code:
          //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          //Phone Number Validation
          //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          function PhoneNumber(StrT,XX){
            StrRE0=/^\d{3}-\d{3}-\d{4}$/; //XXX-XXX-XXXX
            StrRE1=/^1-\d{3}-\d{3}-\d{4}$/; //1-XXX-XXX-XXXX
            StrRE2=/^[(]\d{3}[)]\d{3}-\d{4}$/; //(XXX)XXX-XXXX
            MatchIt=false;
            if(XX==0){if(StrT.match(StrRE0))MatchIt=true;}
            else if(XX==1){if(StrT.match(StrRE1))MatchIt=true;}
            else if(XX==2){if(StrT.match(StrRE2))MatchIt=true;}
            else {if(StrT.match(StrRE0) || StrT.match(StrRE1) || StrT.match(StrRE2))MatchIt=true;}
            
              return MatchIt
          }
          Not sure if this is what you were after.........
          Last edited by A1ien51; Feb 18, 2004, 04:40 PM.
          Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

          Comment


          • #6
            Not sure either...heh heh.
            Anyway, with the phone number below, all parenthesis', white spaces and hyphens would be removed leaving only 10 digits no matter what format they entered it.

            (303) 444-1111 or 303-444-1111 or any other combo
            would come out
            3034441111
            then checked to make sure it's 10 digits...no more...no less.

            I'm not concerned with them accidently messing up. Right now, I'm asking for both phone # and email figuring they'd get at least one right.

            All this is just to keep pranksters from sending bogus forms. I show them the "email sent" page even if it fails validation...but not really sending it. Of course, if it passes validation, I really send the form.

            Thanks all
            Zoobie or not Zoobie...That is the problem.
            <body onUnload="flush( ! )">

            Comment


            • #7
              Sorry, Zoobie. I don't understand how this stops pranksters from sending bogus forms. Surely they can enter any (10-digit) phone number and any email address which is valid in syntax ([email protected]).

              And if I understand you right a genuine user who makes a mistake of some kind is told that his email has been sent when it hasn't. Sounds like a good way to lose customer goodwill to me!

              All the code given in this post has been tested and is intended to address the question asked.
              Unless stated otherwise it is not just a demonstration.

              Comment


              • #8
                Well, most pranksters won't know they have to enter exactly 10 digits...They'll enter a few just to see if the form is sent. Same with the email. This is my thinking anyway...Maybe I'm wrong with this...but it seems to make sense to me.
                Thanks
                Zoobie or not Zoobie...That is the problem.
                <body onUnload="flush( ! )">

                Comment


                • #9
                  It is rather easy

                  just replace all of the occurances of ( ) and - with nothing and the number will be the way that you want it to be....

                  I still do not know why you people are not answering zoobie's question but trying to prove zoobie wrong. There is no way that the phone numer can be valid unless you have a server side program that dials the number and says, yep I am here.....

                  This site is going from helpfull to pestering....

                  Eric
                  Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

                  Comment


                  • #10
                    Well, I may not have the best idea...but nobody has come up with anything better...nor helped...except for the famous a1ien51...which I haven't "seen" in ages...heh heh.

                    Here's the thing...If users are going to order a 3 thousand dollar fence and don't bother to pay attention to their phone number or email, well, all I can say is that's just sad. They really can't be expecting to get the deal the site offers or be scheduled for installation under those circumstances. And if the sale is lost because of this, it's certainly not my fault. If they can't get at least one contact right, something's wrong...and they're probably a prankster anyway.

                    I set up a site for someone where if validation failed, the browser window closed.

                    I saw several regs in here that could have written a quick code stripping all non-digits and white spaces from the input then check that it's 10 digits which is all I wanted. I've been helping out as of late with some easy q's and multi-media ones.

                    Thanks a1ien51...I'll play around with it to see if I can get it going. I'm a bit rusty on js, obviously, but will give it a shot.
                    Last edited by zoobie; Feb 18, 2004, 10:52 PM.
                    Zoobie or not Zoobie...That is the problem.
                    <body onUnload="flush( ! )">

                    Comment


                    • #11
                      Play with this:
                      Code:
                      <script>
                      
                      function fixPhone(unFormat) { 
                        format = unFormat.replace(/\D/gi,"");
                        //format = unFormat.replace(/(\s|\(|\)|-|.)/gi,"");
                        return format;
                      }
                      
                      alert(fixPhone("(111)222-3333"))
                      alert(fixPhone("111-222-3333"))
                      alert(fixPhone("111.222.3333"))
                      alert(fixPhone("111 222 3333"))
                      
                      </script>
                      Eric
                      Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

                      Comment

                      Working...
                      X