cnicollonline

"I was just guessing at numbers and figures, pulling the puzzles apart"

I tend to do a lot of work with database entry through PHP forums. Over time I’ve seen nemorous ways in which someone will enter their phone number.

The Problem

Because there are so many different ways to enter a phone number. How it’s handled, processed and sent to the database becomes a challenge, and as a programmer you have to ask “what is the best way”.

The Solution

The first solution would be to make your column in which will hold the phone number within your database a varchar(20). I suppose for something quick and dirty it will work, but in no means is this efficient.

The second solution would be to take a number:

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

and remove unneeded characters and insert it into the database as a int value of 5555555555

The class:

<?php
/**
* @author: cNicoll
* @dateCreated: 04-23-09
*
* VALIDATES A PHONE NUMBER AND STRIPS THE NUMBER OF ANY UNNEEDED CHARACTERS.
*
* Feel free to use and modify the script in anyway, but I do request that you keep my information
* within the comments. Enjoy!
*
* 2009 © http://www.cnicollonline.com
*/

class PhoneValidate {
	// instance variables
	private $illChars = '/(\W*)/';
	private $validChars = '/^(\W*)[0-9](\W*)/';
	private $number;

	// class constructor
	public function PhoneValidate($number) {
		$this->number = $number;
	}

	/**
	* @param give the number a value
	*/
	public function setNumber($number) {
		$this->number = $number;
	}

	/**
	* @return value of the phone number
	*/
	public function getNumber() {
		return $this->number;
	}

	/**
	* @param Validate input number and convert it
	*/
	public function validate() {
		// if its not null convert it
		if ($this->number != null && preg_match($this->validChars, $this->number)) {
			if (preg_match($this->illChars, $this->number)) {
        		$this->number = preg_replace($this->illChars, '', $this->number);
        		// NOTE: count starts at 0
        		if (strlen($this->number) == 10) {
        			return true;
        		}
        		else {
        			return false;
        		}
    		}
		}
		return false;
	}

}
?>

To use this class:

  1. Import the class
  2. Check to ensure there is a number within the field
  3. Create a new phone validation object and give it a phone number
  4. Run the validation method within an if statement to ensure it worked.
  5. If it did call the get method to retrieve the number.
  6. Close the rest up!
<h3>Validate Phone</h3>
<form METHOD=POST ACTION=demo.php>
Phone Number: <input type="text" name="phone" maxlength="15" size="15">
<input type="submit" value="submit" name="submit">
</form>

<?php
// STEP ONE:
include_once('Phone.validate.class.php');

// STEP TWO:
if (isset($_POST['submit'])) {

	// STEP THREE:
	// is there a number?
	if (isset($_POST['phone'])) {

		// STEP FOUR:
		// create the object
		$phoneValidate = new PhoneValidate($_POST['phone']);

		// STEP FIVE:
		// validate the phone number and remove un wanted characters
		if ($phoneValidate->validate() == true) {

			// STEP SIX:
			echo 'Phone number proccess complete:<br> '.$phoneValidate->getNumber();
		}
		else {
			// error with the characters or length
			echo 'There was an error with this number';
		}
	}

	//
}

?>

Thats just about it. I am open for suggestions or feed back on this snippet.

Write a Comment