tags:

views:

45

answers:

3

i have a phone number in following format +923334173333 and now i want to validate this number exactly in the given format and length with php

A: 

You would need the rules for all possible types of phone numbers and their limitation in the whole world. I'm uncertain if such list even exists.

azatoth
+1  A: 

Fastest way would be

function validatePhoneNumber($n)
{
    return strlen($n) == 13 && $n[0] == '+' && ctype_digit(substr($n, 1)) == 13;
}


validatePhoneNumber('+923334173333'); // returns true
Adam
Don't you want to check if those 13 digits are numbers?
Brendan Long
A: 

You can test the length with strlen(), and use ctype_digit() in combination with substr() to check that the substring starting after the '+' consists only of digits.

greg0ire