View your Shopping CartFrequently Asked QuestionsEmail Us
Home | About Us | Contact Us
SmartWebby.com provides best web design & web programming services globally
We specialize in Rapid Web Design, ASP.net, ASP and PHP web programmingBest Web Design, Best Web Programming, Global Service Provider - Dependable Professionals to build your website right!

International Phone Number Validation in PHP

In this tutorial you will learn about getting International phone numbers and validating them using PHP programming. When phone numbers are obtained from users using form fields, its a good practice to validate the phone number value using PHP code. The following example explains how it can be done for International Phone Numbers.

Example:

International Phone Number Validation

 
Enter your Phone Number Country Code
Area Code
Phone Number


For the above input, the error message generated is as shown below:

Use the sample code given below in your test page and try it yourself.

Sample PHP Code:

<?php
//Check whether the submission is made
if(!isset($_POST["hidSubmit"])){

//Declarate the necessary variables
$intisd="";
$intccode="";
$intphone="";
DisplayForm();
}
else{

//Assign the entered values to variables for validation
$intisd=$_POST["txtisdcode"];
$intccode=$_POST["txtcitycode"];
$intphone=$_POST["txtphone"];

//The entered value is checked for proper format
if(substr_count($intisd,"+")>0){
if(strpos($intisd,"+")==0)
$intisd=substr($intisd,1,strlen($intisd));
}
$result=ereg("^[0-9]+$",$intisd,$trashed);
$result=ereg("^[0-9]+$",$intisd,$trashed);
if(!($result)){echo "Enter Valid ISDCODE";}
$result=ereg("^[0-9]+$",$intccode,$trashed);
if(!($result)){echo "Enter Valid CITY CODE";}
$result=ereg("^[0-9]+$",$intphone,$trashed);
if(!($result)){echo "Enter Valid Phone Number";}
DisplayForm();
}

//User-defined Function to display the form in case of Error
function DisplayForm(){
global $intisd,$intccode,$intphone;

?>

HTML code for the form follows..

Cut 'n' Paste Code for the above Example

HTML CODE

Refer: Explanation of Code

Absolutely FREE Web Templates
Check out these quality free web templates and download them without any registration or sign-up!

FREE Web Design Guide
From web design tips & ideas to HTML, CSS Styles, Fireworks & Dreamweaver you'll find all you need to know about effective web site design right here!

Quality Dreamweaver Templates
Professional quality dreamweaver templates in over 20 categories, starting at just $9.95! Instant download & easy customization