sambayour sambayour - 7 months ago 43
Javascript Question

validate phone number in nigeria and other countries

I need to validate a Nigerian phone number which, by default, will start with 07, 08 or 09 and the total must be 11 digits

e.g(

08030000000
,
07120000000
,
09040000000
) and I also want to validate other countries number without
+
, () or
-
in them e.g(
4433333333
,
232444444444
).

Answer

Well, the pattern is

  ^(07|08|09)[0-9]{9}$

with the simple meaning:

  ^          - string start (anchor)
  (07|08|09) - either 07 or 08 or 09
  [0-9]{9}   - exactly 9 digits (and 11 in total since 07 or 08 or 09 have 2 digits)
  $          - string end (anchor)