shamsuljewel Posted August 28, 2007 Share Posted August 28, 2007 Hi all I want to validate a phone number, I use is_digit() function and it works nice but the problem is if the user want to input multiple phone numbers using , or space it will not work..so what can I do? if I want to check phone number and accept only "," and "space". Quote Link to comment https://forums.phpfreaks.com/topic/67018-phone-number-validation/ Share on other sites More sharing options...
hostfreak Posted August 28, 2007 Share Posted August 28, 2007 I wrote something up for you real quick: <?php if (isset($_POST['Submit'])) { $Phone_Num = mysql_escape_string(trim($_POST['Phone_Num'])); //Assign $_POST value of Phone_Num to variable. Basic input clean $Phone_Num = str_replace(" ", "", "$Phone_Num"); //Get rid of white spaces $Phone_Num = explode(",", $Phone_Num); //Return array of phone numbers $Numbers = array(); foreach ($Phone_Num as $Number) //Loop through $Phone_Num array { if (is_numeric($Number)) //Make sure the phone number is numeric { $Numbers[] = $Number; //Add number to $Numbers array } } echo '<pre>'; print_r($Numbers); echo '</pre>'; } ?> <form name="test" method="POST" action=""> <input name="Phone_Num" type="text"></input> <input name="Submit" type="submit" value="Submit"></input> </form> I'm sure there are more ways to do it, but this works. Edit: A lot more validation etc could be done for this. This should give you the base though. Quote Link to comment https://forums.phpfreaks.com/topic/67018-phone-number-validation/#findComment-336145 Share on other sites More sharing options...
hostfreak Posted August 28, 2007 Share Posted August 28, 2007 Forgot to mention, when you go to add the numbers to the database use the php implode() or join() function to combine them with your desired delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/67018-phone-number-validation/#findComment-336154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.