in reply to Another LostS Question - Phone Number Checks

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"'

Replies are listed 'Best First'.
Re: Re: Another LostS Question - Phone Number Checks
by mandog (Curate) on Aug 23, 2001 at 23:37 UTC
    ...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";'
Re: Re: Another LostS Question - Phone Number Checks
by LostS (Friar) on Aug 23, 2001 at 23:06 UTC
    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"; }
        <soapbox> I agree about the "pet peeve". Often, I try to register on a US site and it won't accept my UK phone number.
        Even UK systems screw up since we often have number changes and the websites often don't change quickly enough.
        Suggestion (adding to comments by [id://kjherron]) :

        Allow any format, strip down to digits, and check for a reasonable number of digits to allow for international dialing codes, etc. then leave it at that.

        If you want real validation, you'll need to check the dialing codes for each potential country to see if they are OK, and KEEP THE DATABASE UP-TO-DATE.
        </soapbox>
        --
        Brovnik