Form Validation in PHP – Simplified

I was created a form earlier today, when I had to add Phone Number validation to the existing validation. This form already included email validation, but I decided to go over it as well.

Email Validation

This is a very similar validation, but there is nothing needed to customize:

function valid_email( $str )
{
    // This checks for "[email protected]"
    return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}

if( ! valid_email( $_POST['email_address'] ) {
    echo "Please enter a valid email address."
}

Phone Number Validation

Edit: Please check out my new post on phone number validation, as it has a much more efficient regular expression pattern.

Phone Number validation is fairly straight forward, but has one major issue – formatting! There are many ways of putting in a phone number, and for usability’s sake, it’s best to supp
ort most (if not all) of them!

Step 1: Deciding the formats you want to support


The most common [North American] phone number formats go as follows:

  • 555-555-5555
  • (555)-555-5555
  • (555) 555-5555
  • 555.555.5555

But as some people like to be creative (or just plain lazy), there can be many other renditions:

  • 5555555555
  • (555)555-5555
  • 555 555 5555

Step 2: Defining your formats in Regular Expression

I’m fairly new to RegEx and all it’s mysteries, but for phone numbers it can be quite simple.

$format1 = "/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i";
//             1-    555-     555-     5555
// or                555-     555-     5555

Here is the above line broken down:
/^([1]-)? – Look from Beginning, and see if there is “1-” 0 or 1 times
[0-9]{3}- – Look for a sequence of characters between 0 and 9, 3 in a row, and add a hyphon (-)
[0-9]{3}- – Look for a sequence of characters between 0 and 9, 3 in a row, and add a hyphon (-)
[0-9]{4} – Look for a sequence of characters between 0 and 9, 4 in a row
$/i – End of String ($), case sensitive (i)

Now the first and last may confuse you, but the middle 3 are the ones we are focusing on here. This RegEx allows any sequence of 1-xxx-xxx-xxxx or xxx-xxx-xxxx, providing all x are numeric.

Adding “-” in the argument allows us to add some formatting to our phone number. We will replace these “-” with any other characters we want to accept.

Step 3: The Code

Here is an example of a condensed phone validation:

function valid_number($phone)
{
    return ( ! preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) && ! preg_match("/^([1]-)?[0-9]{3}.[0-9]{3}.[0-9]{4}$/i", $phone) &&  ! preg_match("/^([1]-)?\([0-9]{3}\) [0-9]{3}-[0-9]{4}$/i", $phone) && ! preg_match("/^[0-9]{10}$/i", $phone)) ? FALSE : TRUE ;
}


Pretty messy, huh? This is a one line argument that will return FALSE if the number entered does not match any of the allowed formats. Let’s clean this up.

$formats = array(
    "/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", // [1-]555-555-5555
    "/^([1]-)?[0-9]{3}.[0-9]{3}.[0-9]{4}$/i", // [1-]555.555.5555
    "/^([1]-)?\([0-9]{3}\)-[0-9]{3}-[0-9]{4}$/i", // [1-](555)-555-5555
    "/^[0-9]{10}$/i", // 5555555555
);

function validate_phone( $phone_number ) {
    foreach( $formats as $format ) {
        // Loop through formats, if a match is found return true
        if( preg_match( $format, $phone_number ) ) return true;
    }
    return false; // If no formats match
}

$valid_phone1 = validate_phone('555-555-5555'); // true

$valid_phone2 = validate_phone('555-555 5555'); // false

The above code is quite straight forward. First, you define all accepted formats in an array. In the validation function, you check to see if the phone number being passed is any of the accept formats. This function returns a boolean value of true or false, making very simple validation:

if( ! validate_phone('555-555-5555' ) {
    // False, return an error
    echo "Please enter a valid phone number.";
}

One thought on “Form Validation in PHP – Simplified”

Leave a Reply

Your email address will not be published. Required fields are marked *