LostS has asked for the wisdom of the Perl Monks concerning the following question:

OK I have this form. It ummm have a phone number field. Using JS I verify that they put in at least 10 characters. Now... in perl I need to check to make sure it isn't 10 characters of nothing... So I need to make sure it is in this format: 222-222-2222 or 222.222.2222 You know Area code - or . 3 numbers - or . then 4 numbers...

Now I know I should use pattern matching with regex however I am not very strong with regex... Do we have a site I can learn it?? I don't wann be asking you all every time I have a regex question...

Thanks
Billy S.
  • Comment on Another LostS Question - Phone Number Checks

Replies are listed 'Best First'.
Re: Another LostS Question - Phone Number Checks
by arturo (Vicar) on Aug 23, 2001 at 22:52 UTC

    general form of the phone number => three digits, followed by a -, followed by three digits, followed by a -, followed by four digits.

    \d matches a single digit. {n} matches the previous match n times (e.g. \w{5} matches five word characters in a row) - outside of [] matches a - (inside [], it specifies a range, e.g. [a-z] matches any lowercase alp +habetic character.)

    the rest is left as an exercise for the reader. Open book exam: perldoc perlre. Good luck, you have fifteen minutes!

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
      ...Open book exam:
               perldoc perlre 

              perldoc perlrequick 

      is a much better tutorial...

      --dan

        Well I do have the Oreilly Perl books on-line... You know the books on CD... I bought it and put it on my web server at home... so I could access it while at work....

        perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat!\n"; } else { print "Thats a dog\n"; } print "\n";'
      Thank You :)

      Here is what I did :)
      if ( $conphone =~ /(\d{3})-(\d{3})-(\d{4})/ ) { print "Phone: $conphone<br />\n"; } else { print "BAD PHONE!!!! BAD PHONE!!!!<br />\n"; }
      It seems to work :) Do you see anythign wrong with that??

      Thanks
      Billy S.
        It's not very user-friendly. You're forcing them to enter their phone number in a particular format, when you could just as easily format the number for them.

        This is a pet peeve of mine. There's more than one way to punctuate phone numbers, and no standardization among web sites:

        (xxx) yyy-zzzz +1 xxx yyy zzzz xxx-yyy-zzzz xxxyyyzzzz
        Even if your instructions are perfectly clear about the expected format, the individual using it may be used to entering it in some other form. Handling multiple formats is much more user-friendly than issuing an error message.

        A simple way to handle this is to accept any old cruft they type, remove everything that isn't a digit, and make sure you got ten digits. You can then format those digits for display or whatever:

        $conphone =~ tr/0-9//dc; # remove non-digits if ($conphone =~ /^(\d{3})(\d{3})(\d{4})$/) { $conphone = $1 . '-' . $2 . '-' . $3; } else { print "Bad phone! No cookie!\n"; }
Re: Another LostS Question - Phone Number Checks
by Maclir (Curate) on Aug 23, 2001 at 22:46 UTC
    I assume you are only getting phone numbers from people in the USA which use that convention for phone numbers. Other countries have different standards. And if you are going to allow phone numbers from other countries, don't forget to allow for the international country code - which is a variable number of digits.
      Only US and only one state (Georgia)
Re: Another LostS Question - Phone Number Checks
by Monky Python (Scribe) on Aug 23, 2001 at 23:14 UTC