Advertisement
gitlez

YA: Simple Phone Number Checking Mod2

May 22nd, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml…
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.     <head>
  5.         <title>Phone numbers</title>
  6.         <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
  7.     </head>
  8. <body>
  9.     <?php
  10.         $phoneNumbers = array("111-1111","(111) 111-1111","111-111-1111","111111",    "111.555.1234","","aaa-aaaa");
  11.  
  12.         foreach ($phoneNumbers as $phone) {
  13.             $failures = 0;
  14.             echo "<hr /><p>Checking '$phone'</p><hr />";
  15.  
  16.             $numNumbers = strlen(preg_replace('/[^0-9]/', '', $phone));
  17.            
  18.             if ( $numNumbers !== 7 && $numNumbers !== 10) {
  19.                 ++$failures;
  20.                 echo "<p><small>Warning: '$phone' must be 7 or 10 characters</small></p>";
  21.             }
  22.  
  23.             if (preg_match("/[^0-9\(\)\-\s\.]+/",$phone)){
  24.                 ++$failures;
  25.                 echo "<p><small>Warning: '$phone' contains non-format characters</small></p>";
  26.             }
  27.             if ($failures == 0) {
  28.                 echo "<p>'$phone' is valid</p>";
  29.             }else{
  30.                 echo "<p>'$phone' is not valid</p>";
  31.             }
  32.         }
  33.     ?>
  34. </body>
  35. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement