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.

 

Comments

Leave a Reply

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

83952