-3

Please I need help to validate phone numbers in various formats

Valid formats:

+111 1 11111111
+11 1111111
1111111
(+111) (11) 1111111

Etc, the format is:

  • You can take a "+" sign only at the beginning and once. Can lead an
  • open and closed parenthesis only the beginning. You can not have more
  • than 15 numbers in total. Can not be less than 8 numbers in total.

Have This:

if(strlen($buff) < 8)
    return false;

$buff = trim(preg_replace('/\s+/', ' ', $buff));

if(preg_match('/^\(\d\) \d \d$/', $buff))
    return $buff;

return false;

Thanks.

e-info128
  • 3,727
  • 10
  • 40
  • 57
  • The common response to this sort of question is "don't do this unless you absolutely have to". Most times project owners think they need it, but are unaware of how much frustration is causes when someone has a real phone number that doesn't fit the pattern, or when someone legitimately does not wish to provide one. – halfer Apr 10 '14 at 15:14
  • (However, if you really must, regular expressions are the way to go). – halfer Apr 10 '14 at 15:15

1 Answers1

1

Smartphone tactics:

  1. remove all non-numeric characters but not the +
  2. replace first sign by 00 if it is a +
  3. replace leading 000 by 00
  4. match possible country code
  5. check length between x and y

You don't even need regex for that.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151