Validation For Mobile Number In PHP
Last Updated : Mar 11, 2024
IN - PHP | Written & Updated By - Riya
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."; } ?>
- The short tags start with "<?" and end with "?>".
- then we use the PHP function valid_phone will check the 10-digit phone number using the preg_match () function.
- then we use preg_match () returns 1 if the pattern matches the given subject, 0 if it does not, or false on failure.
- Then we use the if and else statements.
- 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.