All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Validation For Mobile Number In PHP

Last Updated : Mar 11, 2024

Validation For Mobile Number In PHP

In this article we will show you the solution of validation for mobile number in PHP, inbuilt PHP filters make it easy to develop PHP code that validates phone numbers.

Developers around the world, however, prefer regular expressions. It is possible to parse regular expressions with PHP thanks to its powerful parsing functions. By doing this, you will be able to validate different types of phone numbers effectively.

Using regular expressions, validating phone numbers will be performed.

There are some international phone numbers that require the area code as well as the plus sign.

Thus, you have to make sure it's not filtered out. There is no problem since the filter_var () function supports the + sign. Therefore, you won't need to worry about it being stripped away. Therefore, you won't need to worry about it being stripped away.

We can use this example to validate Indian phone numbers for international users.

We can use this example to validate Indian phone numbers for international users. The number looks like this: +91-202-5555-0234

Step By Step Guide On Validation For Mobile Number In PHP :-

<?php
//code by TalkersCode
function valid_phone($phone)
{
    return preg_match ('/^ [0-9]{10} +$/', $phone);
}
If (valid_phone (9876543210)){
    echo "Your mobile number is valid.";
} else {
    echo "Sorry, your mobile number is invalid.";
}
?>
  1. The short tags start with "<?" and end with "?>".
  2. then we use the PHP function valid_phone will check the 10-digit phone number using the preg_match () function.
  3. then we use preg_match () returns 1 if the pattern matches the given subject, 0 if it does not, or false on failure.
  4. Then we use the if and else statements.
  5. To conclude, we use the echo function to output a string or strings ().

Conclusion :-

The PHP function valid_phone uses the preg_match () function to verify 10-digit phone numbers.

An integer value with a 10-digit regular expression pattern is supplied as a value to the preg_match () function. An integer value with a 10-digit regular expression pattern is supplied as a value to the preg_match () function.

I hope this article on validation for mobile number in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪