Skip to content
Advertisement

PHP preg_match for validating 10 digit mobile number

I am trying to validate 10 digits mobile number using PHP function preg_match. The below code does not produce any output.

Is it the regex wrong? or I am using it incorrectly.

I was expecting Hi True in the output. if it matches or Hi False if it does not match.

JavaScript

regex taken from https://stackoverflow.com/a/7649835/4050261

Advertisement

Answer

The regex you stated will match eleven digits, not ten. Since all Indian mobile numbers start with 9,8,7, or 6, we can use the following regex:

JavaScript

Here is your code snippet updated:

JavaScript

Note that the above regex is still probably far from the best we could do in terms of validation, but it is at least a start.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement