Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Phone Number Validator 2

Status
Not open for further replies.

apw420

MIS
May 12, 2004
6
US
Still working on this script. :) I'm trying to add a phone number validator in now.

I know I need to add this:
if ( $phone =~ / ^ \( \d{3} \) \d{3} - \d{4} $ /x )

But I'm not sure where.

Here's the code:
Code:
#!/usr/bin/perl -w
use CGI ':standard';
use DBI;

$dbh = DBI->connect("DBI:mysql:s04-it604-s13:localhost", "s04-it604-s13", "PASSWORD");
if (param()) {
    $name=param('name'),
    $email=param('email'),
    $phone=param('phone'),
    $time=param('time'),
    $homequestion=param('homequestion'),
    $selected=param('selected'),
    $textarea=param('textarea');
    $query = "INSERT INTO estform (name, email, phone, time, homequestion, selected, textarea ) 
        VALUES ( '$name', '$email', '$phone', '$time', '$homequestion','$selected', '$textarea' )";
    $rows = $dbh->do($query);
}

if ($rows > 0) {
    print redirect(-location=>'[URL unfurl="true"]http://matrix.csis.pace.edu/~s04-it604-s13/Project/thanks.html');[/URL]
}   
else {
    print header();
    print <<EOF
    <html><head><title>Inserting into MySQL
    </title></head><body>
    <h2>Error: INSERT query failed.</h2>
    <a href="javascript:history.go(-1)"><h2>Return to Previous Page</h2></a>
    </body></html>
EOF
}
 
Actually...here's full piece of code I need to enter:

Code:
if ( $phone =~ / ^ \( \d{3} \) \d{3} - \d{4} $ /x ) {

else {
   print( div( { style => "color: red; font-size: x-large" },
             "INVALID PHONE NUMBER" ), br() );

   print( "A valid phone number must be in the form " );
   print( span( { style => "font-weight: bold" },
                  "(555)555-5555." ) );

   print( div( { style => "color: blue" },
             "Click the Back button, and enter a
              valid phone number and resubmit." ) );
   print( br(), br() );
   print( "Thank you." );
}
}
 
your regex needs altering as it won't match anything:-

Code:
@phoneNums = ("(123)456-7890",
              "(123) 567-0987",
              "(123) 456 - 0987");

foreach $phone (@phoneNums) {
  if ( $phone =~ /^\(\d{3}\)\d{3}-\d{4}$/ ) {
    print " matched : $phone\n"; 
  } else {
    print "no match : $phone\n"; 

  }
}

this test gives the output:-

Code:
[red] matched : (123)456-7890
no match : (123) 567-0987
no match : (123) 456 - 0987[/red]


Kind Regards
Duncan
 
take a few minutes to understand the regex:-

/^\(\d{3}\)\d{3}-\d{4}$/

Code:
/ [red]<--- start of regex[/red]
^ [red]<--- beginning of line[/red]
\( [red]<--- open paren[/red]
\d{3} [red]<--- 3 digits[/red]
\) [red]<--- close paren[/red]
\d{3} [red]<--- 3 digits[/red]
- [red]<--- hyphen[/red]
\d{4} [red]<--- 4 digits[/red]
$ [red]<--- end of line[/red]
/ [red]<--- end of regex[/red]


Kind Regards
Duncan
 
extending the regex like this would be useful:-

Code:
undef $/;

$text = <DATA>;

do {
  print "$1 $2 $3\n";
}
while ($text =~ /\((\d{3})\) ?(\d{3}) ?- ?(\d{3,4})/g);

__DATA__
(123)456-7890
(123) 567-0987
(123) 456 - 0987
blah blah blah (123) 456 - 0987 blah blah blah (345) 555 - 677 blah blah blah

gives output:-

Code:
[red]123 456 7890
123 567 0987
123 456 0987
123 456 0987
345 555 677[/red]


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top