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
2010-05-30 21:14:56
+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
2010-05-30 21:16:11
Don't you want to check if those 13 digits are numbers?
Brendan Long
2010-05-30 21:17:10
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
2010-05-30 21:16:43