DEV Community

mhsohag11
mhsohag11

Posted on

Answer: PHP preg_match for validating 10 digit mobile number

The following code snippet will check if the mobile number digits are within 10-15 digits including '+' at the start and followed by a non-zero first digit.

Regular expression

"/^[+]?[1-9][0-9]{9,14}$/"

Code snippet

// Validation for the mobile field.
function validateMobileNumber($mobile) {
  if (!empty($mobile)) {
    $isMobileNmberValid = TRUE;
    $mobileDigitsLength = strlen($mobile);

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

I don't think that's a particularly good answer, or something that can be generalised like that.
It presumes not only the things the asker is looking for - a number is 11 or 15 digits long and may or may not start with a + - but that the first digit isn't zero.

Most people in my country (UK) will enter their mobile number starting 07, because that's the most common prefix and 0 means "stand by telephone exchange because I'm about to hit you with a national number".

The trouble is, not all phones are mobile, not all mobiles have to start with a 7, people have redirected numbers in the same way they might have P.O. Boxes, and people may or may not want to use an internationalised number.

I guarantee any site using a regex (or expanded set of rules) like this one will have a small number of annoyed users who can't seem to put in their perfectly legitimate number.