How To Validate Indian Mobile Phone Numbers in PHP

Reading Time: 4 minutes
132 Views

Validating Indian mobile phone numbers is a crucial step in ensuring data accuracy and integrity in web applications targeting the Indian audience. In PHP, validating Indian mobile numbers involves verifying their format and adherence to Indian numbering standards.

In this tutorial, we’ll see the comprehensive process of validating Indian mobile phone numbers using PHP. This functionality empowers developers to create robust validation mechanisms that accurately confirm the authenticity of mobile numbers entered in Indian formats.

Read More: How To Send Emails Using GMail SMTP and PHPMailer 6

Let’s get started.

Setup PHP Helper Function for Mobile Numbers

Create a standalone helper function to validate Indian mobile phone numbers,

function validateIndianMobileNumber($number) {
    // Regular expression for Indian mobile numbers with or without country code
    $pattern = '/^(?:\+?91|0)?[6-9]\d{9}$/';

    // Check if the number matches the pattern
    if (preg_match($pattern, $number)) {
        return true; // Valid Indian mobile number
    } else {
        return false; // Invalid Indian mobile number
    }
}

Explanation of the regular expression /^(?:+?91|0)?[6-9]\d{9}$/

  • ^ asserts the start of the string.
  • (?:+?91|0)? matches an optional country code: either +91, 91, or 0 (for numbers without the country code).
  • [6-9] matches the first digit after the country code or without it, which must be 6, 7, 8, or 9.
  • \d{9} matches exactly 9 digits (0-9).
  • $ asserts the end of the string.

This function validateIndianMobileNumber() will validate Indian mobile numbers regardless of whether they have a country code (+91, 91) or not.

Pass your variable $mobileNumber to test different phone numbers.

Test Cases of Indian Mobile Phone Numbers

We will check with 5 different cases,

Case #1 (Mobile: 6876543210)

$mobileNumber = "6876543210";

Use

if (validateIndianMobileNumber($mobileNumber)) {
    echo "Valid Indian mobile number!";
} else {
    echo "Invalid Indian mobile number!";
}

Output

Valid Indian mobile number!

Read More: What is CSRF Token and How To Use in Core PHP Tutorial

Case #2 (Mobile: 5897419632)

$mobileNumber = "5897419632";

Use

if (validateIndianMobileNumber($mobileNumber)) {
    echo "Valid Indian mobile number!";
} else {
    echo "Invalid Indian mobile number!";
}

Output

Invalid Indian mobile number!

Case #3 (Mobile: +17837085989)

$mobileNumber = "+17837085989";

Use

if (validateIndianMobileNumber($mobileNumber)) {
    echo "Valid Indian mobile number!";
} else {
    echo "Invalid Indian mobile number!";
}

Output

Invalid Indian mobile number!

Case #4 (Mobile: 08948489931)

$mobileNumber = "08948489931";

Use

if (validateIndianMobileNumber($mobileNumber)) {
    echo "Valid Indian mobile number!";
} else {
    echo "Invalid Indian mobile number!";
}

Output

Valid Indian mobile number!

Case #5 (Mobile: +918847489933)

$mobileNumber = "+918948489931";

Use

if (validateIndianMobileNumber($mobileNumber)) {
    echo "Valid Indian mobile number!";
} else {
    echo "Invalid Indian mobile number!";
}

Output

Valid Indian mobile number!

That’s it.

We hope this article helped you to learn about How To Validate Indian Mobile Phone Numbers in PHP Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more