Mobile number validation pattern in PHP

Viewed 101521

I am unable to write the exact pattern for 10 digit mobile number (as 1234567890 format) in PHP . email validation is working.

here is the code:

function validate_email($email)
{
return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]    {2,6}$", $email);
}

function validate_mobile($mobile)
{
  return eregi("/^[0-9]*$/", $mobile);
}
3 Answers

You can use this regex below to validate a mobile phone number.

\+ Require a + (plus signal) before the number
[0-9]{2} is requiring two numeric digits before the next
[0-9]{10} ten digits at the end.
/s Ignores whitespace and break rows.

$pattern = '/\+[0-9]{2}+[0-9]{10}/s';

OR for you it could be:

$pattern = '/[0-9]{10}/s';

If your input text won't have break rows or whitespaces you can simply remove the 's' at the end of our regex, and it will be like this:

$pattern = '/[0-9]{10}/';

For India : All mobile numbers in India start with 9, 8, 7 or 6 which is based on GSM, WCDMA and LTE technologies.

function validate_mobile($mobile)
{
    return preg_match('/^[6-9]\d{9}$/', $mobile);
}

if(validate_mobile(6428232817)){
    echo "Yes";
}else{
    echo "No";
}

// Output will Yes

Related