Jump to content

Please help with US phone number validation


damion

Recommended Posts

I have a form in which I want it to accept US phone formats, ie (123) 123-1234, 123-123-1234, 1-123-123-1234, etc..

 

Below is my code and I would really appreciate it if someone can tell me what I need to accept the #s.  Thank you!

    $session = $_SESSION['verify'];
    $error = '';

        if(trim($name) == '') {
            $error .= '<li>Your name is required.</li>';
        }
        
        if(trim($email) == '') {
            $error .= '<li>Your e-mail address is required.</li>';
        } elseif(!isEmail($email)) {
            $error .= '<li>You have entered an invalid e-mail address.</li>';
        }
        
        if(trim($phone) == '') {
            $error .= '<li>Your phone number is required.</li>';
        } elseif(!is_numeric($phone)) {
            $error .= '<li>Your phone number can only contain digits.</li>';
        }
        
        if(trim($comments) == '') {
            $error .= '<li>You must enter a message to send.</li>';
        }
        
        if($session != $verify) {
            $error .= '<li>The verification code you entered is incorrect.</li>';
        }
        
        if($error != '') { 
            echo '<div class="error_message">Attention! Please correct the errors below and try again.';
            echo '<ul class="error_messages">' . $error . '</ul>';
            echo '</div>';
        
        } else {
        
        if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }

Yeah, you're going to have to make a decision. Are you going to require the user to use specific patterns as you showed in your example? If so, you will need to build a list of those patters to compare against. Or you can just accept one pattern and ensure the form gives an example of the pattern that is required.

 

However, I think a better approach is to not require a pattern, but to just identify if the digits can be used to make up a phone number. Here are the rules I would follow (this assumes you require the area code):

 

1. Remove all non-digit characters from the input.

2. Is the remaining length 10 characters (it is valid)

3. Is the remaining length 11 characters and the first character is a '1' (it is valid)

 

If the input is valid then I would format it to MY preferenced format.

 

Example:

$stripped_input = preg_replace('#[^\d]#', '', $input);
if(strlen($stripped_input)==10 || (strlen($stripped_input)==11 && $stripped_input[0]=='1'))
{
    $format = (strlen($stripped_input)==10)
    if (strlen($stripped_input)==10) ? : 
    {
        $formatted_input = preg_replace('#(\d\d\d)(\d\d\d)(\d\d\d\d)#', '$1-$2-$3', $stripped_input);
    }
    else
    {
        $formatted_input = preg_replace('#(\d)(\d\d\d)(\d\d\d)(\d\d\d\d)#', '$1-$2-$3-$4', $stripped_input);
    }
    echo "Valid: {$formatted_input}";
}
else
{
    echo "Invalid: {$input}";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.