US phone number validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JackM

    US phone number validation

    I'm still working on validating the phone numbers that are entered on a
    form but have come across a problem I don't understand how to fix. I can
    handle most instances when it's in regular US formats (555-555-5555 or
    (555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is
    ten consecutive numbers with nothing else (5555555555). Not only does it
    not validate, but it shows as 2147483647 when I call the variable to
    check to see how it is set. I know I could break the form up into three
    values as in <input name="phone[area]"> <input name="phone[code]"> and
    <input name="phone[num]"> to eliminate that happening but I don't like
    the look of that and I know there's a better way that I haven't found.

    I tried googling for information on a preg_replace statement that could
    do what I need (check the phone number and add a space between the 3rd
    and 4th and the 6th and 7th digits if there isn't one there already) but
    I haven't been successful.

    What do I need to do to handle this?
  • Gordon Burditt

    #2
    Re: US phone number validation

    >I'm still working on validating the phone numbers that are entered on a[color=blue]
    >form but have come across a problem I don't understand how to fix. I can
    >handle most instances when it's in regular US formats (555-555-5555 or
    >(555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is
    >ten consecutive numbers with nothing else (5555555555). Not only does it
    >not validate, but it shows as 2147483647 when I call the variable to
    >check to see how it is set.[/color]

    Telephone numbers are not integers. They are strings. In particular,
    leading zeroes count. Don't treat them as numbers. Don't print
    them using numeric formats. A 10-digit phone number may not fit in
    a 32-bit integer. Although you won't find US phone numbers beginning
    with zero, on international calls from the US it does matter whether
    you dial a leading zero or not. Also, there's a difference between
    817-555-0001 and 817-555-1.

    US telephone numbers also occasionally have extensions, which makes
    them longer than 10 digits.
    [color=blue]
    >I know I could break the form up into three
    >values as in <input name="phone[area]"> <input name="phone[code]"> and
    ><input name="phone[num]"> to eliminate that happening but I don't like
    >the look of that and I know there's a better way that I haven't found.[/color]

    My preference would be to STORE it as a digit string (varchar) and
    display it in some standardized format I like. On input, it's probably
    best to first strip out all the non-digits and then validate it.
    [color=blue]
    >I tried googling for information on a preg_replace statement that could
    >do what I need (check the phone number and add a space between the 3rd
    >and 4th and the 6th and 7th digits if there isn't one there already) but
    >I haven't been successful.[/color]

    Gordon L. Burditt

    Comment

    • Richard Levasseur

      #3
      Re: US phone number validation

      ^(\D*)?(\d{3})( \D*)?(\d{3})(\D *)?(\d{4})$

      if my regex memory serves correctly:
      That will match xYYYxYYYxYYYY, where each x is a non-digit and is
      optional, and Y is a digit. Group 2, 4, and 6 are the area code,
      prefix, and suffix, respectively.

      1234567890 - match
      (123) 456-7890 - match
      A123BCD4567890 - match
      123-456-7890 - match

      Another option is to strip all non-digits and see if its 10 characters
      long:
      regex: \D, with the global modifier on (preg_replace_a ll i think)

      Store phone numbers as strings with all non-digits stripped out, then
      display them how you like.

      Comment

      • NC

        #4
        Re: US phone number validation

        JackM wrote:[color=blue]
        >
        > I'm still working on validating the phone numbers[/color]
        ....[color=blue]
        > What do I need to do to handle this?[/color]

        Strip every non-digit from the string containing the phone number and
        make sure that there are exactly ten symbols left; additionally, in the
        U.S., the first digit cannot be 0 or 1.

        Cheers,
        NC

        Comment

        • Richard Levasseur

          #5
          Re: US phone number validation

          Are you sure about that? Is 1 not the US/CA country code? I can dail
          full 10 digit numbers (1xxxyyyzzzz) without any problem in the US

          Comment

          • Jerry Stuckle

            #6
            Re: US phone number validation

            Richard Levasseur wrote:[color=blue]
            > Are you sure about that? Is 1 not the US/CA country code? I can dail
            > full 10 digit numbers (1xxxyyyzzzz) without any problem in the US
            >[/color]

            Richard,

            That's 11 digits :-)

            And it's general practice in the U.S. to leave the '1' off the phone number.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • NC

              #7
              Re: US phone number validation

              Richard Levasseur wrote:[color=blue]
              >
              > Are you sure about that? Is 1 not the US/CA country code?[/color]

              Not exactly. When you call from outside North America, 1 is indeed the
              U.S. country code. When you call from the U.S. and the first number
              you dial is 1, it's a signal for the telephone exchange to switch from
              local to long-distance dialing. The exchange assumes that the 10
              digits after the 1 are a three-digit area code followed by a
              seven-digit phone number. The single system of area codes covers the
              U.S., Canada, Mexico, and several island nations in the Caribbean.
              Essentially, if you are calling from, say, New York, dialing
              1xxxyyyzzzz can get you to Chicago, Montreal, Mexico City, or Grand
              Cayman.

              Similarly, 0 signals the caller's intent to place an international
              call. This is why area codes do not start with either 1 or 0.
              [color=blue]
              > I can dail full 10 digit numbers (1xxxyyyzzzz) without any
              > problem in the US[/color]

              Yes, but this is 11 digits. :)

              Cheers,
              NC

              Comment

              • Tim Streater

                #8
                Re: US phone number validation

                In article <DYGdncLwGPLF-6PZRVn-rw@comcast.com> ,
                Jerry Stuckle <jstucklex@attg lobal.net> wrote:
                [color=blue]
                > Richard Levasseur wrote:[color=green]
                > > Are you sure about that? Is 1 not the US/CA country code? I can dail
                > > full 10 digit numbers (1xxxyyyzzzz) without any problem in the US
                > >[/color]
                >
                > Richard,
                >
                > That's 11 digits :-)
                >
                > And it's general practice in the U.S. to leave the '1' off the phone number.[/color]

                I take it you mean, when saying that, the number as written down. I'm
                not sure I agree. When I lived there I kept seeing and hearing ads that
                said as it might be "Call one-eight-hundred .... now for your ...".

                -- tim

                Comment

                • Jerry Stuckle

                  #9
                  Re: US phone number validation

                  Tim Streater wrote:[color=blue]
                  > In article <DYGdncLwGPLF-6PZRVn-rw@comcast.com> ,
                  > Jerry Stuckle <jstucklex@attg lobal.net> wrote:
                  >
                  >[color=green]
                  >>Richard Levasseur wrote:
                  >>[color=darkred]
                  >>>Are you sure about that? Is 1 not the US/CA country code? I can dail
                  >>>full 10 digit numbers (1xxxyyyzzzz) without any problem in the US
                  >>>[/color]
                  >>
                  >>Richard,
                  >>
                  >>That's 11 digits :-)
                  >>
                  >>And it's general practice in the U.S. to leave the '1' off the phone number.[/color]
                  >
                  >
                  > I take it you mean, when saying that, the number as written down. I'm
                  > not sure I agree. When I lived there I kept seeing and hearing ads that
                  > said as it might be "Call one-eight-hundred .... now for your ...".
                  >
                  > -- tim[/color]

                  Tim,

                  Yes, when you tell someone to call a long distance number, you generally say
                  "one eight hundred". However, in forms it's generally not used.

                  For instance - here in the MD side of Washington, DC, I can call anyone in the
                  202 area code as a local call. I can also call some of the 301, 240 and 703
                  area codes as local, while others are long distance. I need to dial 1 for those
                  which are long distance, but do not dial 1 for those which are local.

                  Someone on the Virginia side of DC has a different combination, and those in DC
                  a still different one. Do you dial 1 or not?

                  So it has become the general procedure to document numbers without the '1' and
                  let the dialer figure out whether they need to dial '1' or not. The only
                  exception (which is what I think you're referring to) is free area codes such as
                  800, 877, 866, etc., which are always long distance. Then you'll hear on the TV
                  and radio "Dial one-eight-hundred...".


                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Jambalaya

                    #10
                    Re: US phone number validation

                    JackM wrote:[color=blue]
                    > I'm still working on validating the phone numbers that are entered on a
                    > form but have come across a problem I don't understand how to fix. I can
                    > handle most instances when it's in regular US formats (555-555-5555 or
                    > (555) 555-5555 or 555 555 5555) but I'm having trouble when the entry is
                    > ten consecutive numbers with nothing else (5555555555). Not only does it
                    > not validate, but it shows as 2147483647 when I call the variable to
                    > check to see how it is set. I know I could break the form up into three
                    > values as in <input name="phone[area]"> <input name="phone[code]"> and
                    > <input name="phone[num]"> to eliminate that happening but I don't like
                    > the look of that and I know there's a better way that I haven't found.
                    >
                    > I tried googling for information on a preg_replace statement that could
                    > do what I need (check the phone number and add a space between the 3rd
                    > and 4th and the 6th and 7th digits if there isn't one there already) but
                    > I haven't been successful.
                    >
                    > What do I need to do to handle this?[/color]

                    This regex has worked pretty well for me. You can comment out the last
                    seperator and extension line if you don't need that.

                    $rgx = '/^'; //beginning of string
                    $rgx .= '(?:\([2-9]\d{2}\)\ ?'; //either (200-999) with optional space
                    $rgx .= '|'; //or
                    $rgx .= '[2-9]\d{2}[- \.]?)'; //200-999 with optional seperator '- .'
                    $rgx .= '[2-9]\d{2}'; //middle 4 digits 200-999
                    $rgx .= '[- \.]?'; //optional seperator '- .'
                    $rgx .= '\d{4}'; //last 4 digits 0000-9999
                    $rgx .= '[- \.]?'; //optional seperator '- .'
                    $rgx .= '(?:x|ext)?\.?\ ?\d{0,5}'; //optional extension
                    $rgx .= '$/'; //end of string

                    if (!preg_match($r gx, $userinput)){
                    $this->addError('Th is must be a valid phone number.');
                    }

                    If your variable is cast as an integer then the largest integer value
                    PHP can handle is 2147483647. PHP generally automatically switches to
                    float representations if your value is 2^32 or larger.[1] I store it as
                    a string to avoid problems of that sort...


                    [1]<url:http://us3.php.net/manual/en/language.types. integer.php>

                    Comment

                    • Michael Trausch

                      #11
                      Re: US phone number validation

                      NC wrote On 04/13/2006 12:02 PM:[color=blue]
                      > Richard Levasseur wrote:[color=green]
                      >> Are you sure about that? Is 1 not the US/CA country code?[/color]
                      >
                      > Not exactly. When you call from outside North America, 1 is indeed the
                      > U.S. country code. When you call from the U.S. and the first number
                      > you dial is 1, it's a signal for the telephone exchange to switch from
                      > local to long-distance dialing. The exchange assumes that the 10
                      > digits after the 1 are a three-digit area code followed by a
                      > seven-digit phone number.
                      >[/color]

                      Almost, but it is a bit more complex then that. If you start your
                      number with '10', the next three digits are then assumed to be the
                      actual long distance carrier that you wish to use for placing a long
                      distance call. So the number string becomes 15 digits. Also, this only
                      works from a POTS landline. It could be used in a database to indicate
                      that it's cheaper to use AT&T, say, in one particular special example,
                      though it probably wouldn't be useful enough to figure in. Nonetheless,
                      I have had people that have fed me phone numbers in that crazy format
                      before and it was confusing. (Strange people.)

                      - Mike

                      Comment

                      Working...