You need to list each unique property of a valid phone number, then test your phone number variable for those properties using php. With what you have so far, these are the properties I see: 1. 12 characters in length. 2. 4th and 8th characters are spaces or hyphens. Both of those are trivially easy to test for in php. However, I think you need to think about your properties more--see if you can create an invalid phone number that has all your properties. Is 123-abc-@#$% a valid phone number? If not, then you need to define and check for invalid characters. Is 7128675309 a valid phone number even though its only 10 characters long? If so, refine properties #1 & #2. Is 712.867.5309 a valid phone number even though it uses periods? If so, refine property #2. Keep trying to break your properties with invalid phone numbers until your properties are satisfactory and then code a test for each property in php.
class phoneNumber { public $nums; function __construct($num) { $this->set($num); } function set($num) { $num = preg_split('//', $num); foreach ($num as $key => $value) if (!is_numeric($value)) unset($num[$key]); if (count($num) < 10 OR count($num) > 11) { trigger_error("WOOPS! The phone number is not valid. The length ".count($num)." isn't enough."); exit; } $num = array_merge(array(), $num); $this->nums = $num; } function get() { $nums = $this->nums; if (count($nums) == 11) { $num = $nums[1].$nums[2].$nums[3]."-".$nums[4].$nums[5].$nums[6]."-".$nums[7].$nums[8].$nums[9].$nums[10]; } else if (count($nums) == 10) { $num = $nums[0].$nums[1].$nums[2]."-".$nums[3].$nums[4].$nums[5]."-".$nums[6].$nums[7].$nums[8].$nums[9]; } return $num; } } PHP: I just wrot ethis class for 'ya. Here's the usage: $somePhoneNumber = "1 234 567 8901" $pn = new phoneNumber($somePhoneNumber); echo $pn->get(); PHP: All it does is store all the numbers inside the input into an array. If the array isn't the right size for a phone number (10 or 11), then it uses trigger_error() to show an error message. The get() function displays the formatted phone number which is just the array formatted as a phone number.
Nice! Thanks! Now umm how do i go about adding that to my code? Sorry kinda a php newbie here... Thanks though that's awesome!!!
Just add the class declaration somewhere above the code. In a global file if possible. Then you can text a phone number by storing whatever you want to test in $somePhoneNumber , then testing it with: $pn = new phoneNumber($somePhoneNumber); $result = $pn->get(); Code (markup): $result is what you should actually add to your database. $result stores the properly formatted phone number. The script formats all phone numbers the same way so that everything will be consistent.