1

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 Please enter a valid phone number . 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. Example

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: (123)456-7890?

PHP:

if (isset($_POST['phone'])) {
    if(preg_match('/^(\({1}\d{3}\){1}|\d{3})(\s|-|.)\d{3}(\s|-|.)\d{4}$/',$phone)) {
        echo ('<div id="phone_input"><span id="resultval">'.$phone.'</span></div>');
    }
    else {
        echo '<div id="phone_input"><span id="resultval">Please enter a valid phone number</span></div>';
    }
}
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
  • Just remove nondigits and format it as you want to format it. Lazy people like me don't like having to type in the punctuators. – James McNellis Jul 28 '12 at 23:37

1 Answers1

5

Try This

<?php
class Validation {
    public $default_filters = array(

        'phone' => array(
            'regex'=>'/^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})$/',
            'message' => 'is not a valid US phone number format.'
        )
    );
    public $filter_list = array();

    function Validation($filters=false) {
        if(is_array($filters)) {
            $this->filters = $filters;
        } else {
            $this->filters = array();
        }
    }

    function validate($filter,$value) {
        if(in_array($filter,$this->filters)) {
            if(in_array('default_filter',$this->filters[$filter])) {
                $f = $this->default_filters[$this->filters[$filter]['default_filter']];
                if(in_array('message',$this->filters[$filter])) {
                    $f['message'] = $this->filters[$filter]['message'];
                }
            } else {
                $f = $this->filters[$filter];
            }
        } else {
            $f = $this->default_filters[$filter];
        }
        if(!preg_match($f['regex'],$value)) {
            $ret = array();
            $ret[$filter] = $f['message'];
            return $ret;
        }
        return true;
    }
}

//example usage
$validation = new Validation();
echo nl2br(print_r($validation->validate('phone','555-555-1212'),true));
echo nl2br(print_r($validation->validate('phone','(555)-555-1212'),true));
echo nl2br(print_r($validation->validate('phone','555 555 1212'),true));
echo nl2br(print_r($validation->validate('phone','555.555.1212'),true));
echo nl2br(print_r($validation->validate('phone','(555).555.1212'),true));
echo nl2br(print_r($validation->validate('phone','(555)---555.1212'),true));//will not match
?>
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • But what if someone likes to write their phone number as 2234567890 or (223)-555-9191 or (445) 333-6969? – Bailey Parker Jul 28 '12 at 23:44
  • Why the `if(x) return true; else return false;`? Just `return x;`. (Not that this should ever be used to validate phone numbers anyways...) – Ry- Jul 28 '12 at 23:50
  • +1 @AbidHussain Thank you, I am new to php. how would i call the `class Validation` inside my code? –  Jul 29 '12 at 00:50
  • class code copy and paste another file just like validation.php. after then include validation.php top side in your file. ex. include "validation.php"; after then use //example usage code – Abid Hussain Jul 29 '12 at 08:38