PHP: Validation of US Phone numbers
<p>I am currently working with validation of US phone numbers. The issue is that code below is always echoing after a valid or invalid input <code>Please enter a valid phone number</code> . My basic logic of my code is that I am checking with the preg_match to see if there is match for a valid number. <a href="http://webprolearner.ueuo.com/dropdown-menu/test1.php" rel="nofollow"><strong>Example</strong></a></p>

<p>How can I fix this or is there a better way to validate phone numbers?
Also, Is there away to echo the number formatted like this: <code>(123)456-7890</code>? </p>

<p><strong>PHP</strong>:</p>

<pre><code>if (isset($_POST['phone'])) {
    if(preg_match('/^(\({1}\d{3}\){1}|\d{3})(\s|-|.)\d{3}(\s|-|.)\d{4}$/',$phone)) {
        echo ('&lt;div id="phone_input"&gt;&lt;span id="resultval"&gt;'.$phone.'&lt;/span&gt;&lt;/div&gt;');
    }
    else {
        echo '&lt;div id="phone_input"&gt;&lt;span id="resultval"&gt;Please enter a valid phone number&lt;/span&gt;&lt;/div&gt;';
    }
}
</code></pre>
