php code to validate phone number

How to validate phone number using php?

PHP web developers need to validate the phone number for contact us , registration, profile,  login web page form etc.

We must have to confirm that the phone number submitted by user is in the valid structure or pattern.

We know that phone number is generally a 10 digits number. So We must validate number with integer of 10 in length.

We can use php function preg_match() to validate 10-digit mobile numbers.

We can use below customized php function code to validate the 10 digit phone number.

<?php
//code by aryatechno
function valid_phone($phone)
{
    return preg_match('/^[0-9]{10}+$/', $phone);
}
if(valid_phone(9428982251)){
    echo "Your phone number is valid.";
}else{
    echo "Sorry, Your phone number is invalid.";
}
?>

Output :

Your phone number is valid.

PHP function valid_phone will check 10 digit phone number using preg_match() function.

preg_match() function contains regular expression pattern for 10 digit integer value.

 

Example :

<?php
//code by aryatechno
function valid_phone($phone)
{
return preg_match('/^[0-9]{10}+$/', $phone);
}
$phone1="9428982251";
if(valid_phone($phone1)){
echo "<br>Your phone number : $phone1 is valid.";
}else{
echo "<br>Sorry, Your phone number : $phone1 is invalid.";
}

$phone2="82251";
if(valid_phone($phone2)){
echo "<br>Your phone number : $phone2 is valid.";
}else{
echo "<br>Sorry, Your phone number : $phone2 is invalid.";
}

?>

Output :

Comments

Ghgh

Kavita

Hddjsnsjdhdbsn

Gg

Ff

Gg

Leave a Reply

Your email address will not be published. Required fields are marked *

52430