This article includes the PHP class I use for simple form validation. The intent is to use the class for simple web forms such as a contact us form. If you are working on something larger like an e-commerce site you need something more robust.

Remember, using JavaScript will cut down on your server load, but always have a server side backup.

This is a work in progress, and not fully tested. If you have any suggestions or fixes please let me know and I'll mend immediately.

How To Use

Copy the below code into validator.php

Include the file in your script

require_once("validation.php");

Call any of the functions like so, documentation is included above each function

$validate = new Validation;
$validate->isEmail($email);
$validate->isPhone($phone);
$validate->isPostalCode($postal);
$validate->isZipCode($zip);

The Class

<?php
/**
* Validation Class
* v1.2
* Last Updated: Jan 10, 2010
* URL: http://www.nickyeoman.com/blog/php/104-php-validation-class
**/


class Validation {

	/**
	* If an email is Valid it returns the parameter
	* other wise it will return false 
	* $email is the email address
	**/
	function isEmail($email) {
		//email is not case sensitive make it lower case
		$email =  strtolower($email);

		//check if email seems valid
		if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) 
			return $email;
		else
			return false;
	}
	
	/**
	* Checks if there are at least 10 numbers, if so returns cleaned parameter
	* other wise it will return false 
	* $phone is the phone number
	* $ext if set to true return an array with extension separated
	**/
	function isPhone($phone, $ext = false) {
		
		//remove everything but numbers
		$numbers = ereg_replace("[^0-9]", "", $phone );
		
		//how many numbers are supplied
		$length = strlen($numbers);
		
		if($length == 10) {
			$cleanPhone = $numbers;
			return $cleanPhone;
		}elseif ($length > 10) {
			if (substr($numbers,0,1) == 1) {
				$clean['phone'] = substr($numbers,0,11);
				$clean['extension'] = substr($numbers,11);
				if (!$ext)
					if (!empty($clean['extension']))
						return implode("x",$clean);
					else
						return $clean['phone'];
				else
					return $clean;
			
			} else {
				$clean['phone'] = substr($numbers,0,10);
				$clean['extension'] = substr($numbers,10);
				if (!$ext)
					return implode("x",$clean);
				else
					return $clean;
			}
		} else {
			return false;
		}

	}
	
/**
* Canadian Postal code
* thanks to: http://roshanbh.com.np/2008/03/canda-postal-code-validation-php.html
**/
	function isPostalCode($postal) {
		 if(preg_match("/^([a-ceghj-npr-tv-z]){1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}$/i",$postal))
    return true;
 else
    return false;

	}
	
	/** Checks for a 5 digit zip code
	*  Clears extra characters
	* returns clean zip
	**/
	function isZipCode($zip) {
		//remove everything but numbers
		$numbers = ereg_replace("[^0-9]", "", $zip );
		
		//how many numbers are supplied
		$length = strlen($numbers);
		
		if ($length != 5) {
			return false;
		} else {
			return $numbers;
		}
		
	}

}
/** End Validation **/

Reference

Comments

Show/Hide Comment form