Regular Expression Phone Number Verification in PHP

Very simple regular expression phone number verification function (U.S. and Canada numbers, anyway)

I built this to deal with the structure of U.S. phone numbers and their variations. Of course, if doesn't tell you if it is a working number. This simply checks to make sure that the phone number has a 3-digit area code (optional, but 3-digit) (with optional parentheses), 3-digit prefix, and a 4-digit suffix. It allows you to place periods, dashes, or spaces where you would expect to be able to.

PHP Source Code

<?php

// isValidPhone( [string] )
//    returns 0 if phone number is invalid
//    returns 1 if phone number is good
function isValidPhone( $p ) {
    return preg_match( '^(\(?[0-9]{3}\)?)?[ .-]?[0-9]{3}[.-]?[0-9]{4}$', $p )
}

$msg = '';
if( !empty( $_POST['phone'] ) ) {
    if( isValidPhone( $_POST['phone'] ) ) {
        $msg = '<p style="color:#009900">Good phone number!</p>';
    } else {
        $msg = '<p style="color:#990000">Bad phone number.</p>';
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" version="-//W3C//DTD XHTML 1.1//EN" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
    <link href="/includes/css/ozStyle.css.php" type="text/css" rel="stylesheet" />
    <title>Regular Expression Phone Number Validation Demo in PHP</title>
</head>

<body style="margin:10px">

<h1>Phone Number Validation</h1>

<?= !empty( $msg ) ? $msg : '' ?>

<form method="post" style="margin: 0px; padding: 0px;">

    <p>Phone Number:<br />
    <input name="phone" type="text" value="<?=$_POST['phone']?>" size="20" maxlength="20"></p>

    <p><input type="submit" value="submit" /></p>

</form>


</body>

</html>