Recent Post

Dear reader, this blog has been moved to here

Sunday, 8 June 2014

How to Validate Nigerian Phone Number Using PHP



I was on a project that an SMS is required to be sent to a subscriber which must be a Nigerian Mobile Number in other to manage their SMS Units, that is what led me into writing a function to validate Nigerian phone numbers.



This is a simple PHP function which validates a phone number to reach the required standard of:

  • Must be numeric

  • Must start from 080, 090, 070 and 081 right?

  • Must be 11 digits in length


Now let's go, this my custom function returns 5 outputs which are:

  • 111 = input is not a digit

  • 110 = not starting from 080, 070, 081, 090

  • 120 = not 11 characters

  • 200 = its okay

  • 0 = empty input


Copy out the below function
<?php
function check_number($number) {
/*
111 = input is not a digit
110 = not starting from 080, 070, 081, 090
120 = not 11 characters
200 = its okay
0 = empty input
*/

//Lets really know if the input is not empty, which if it is, return false
if(!$number) {
return false;
}

//Checking if its really numerics
elseif(!is_numeric($number)) {
return 111;
}

//Checking if number starts with 080, 090, 070 and 081
elseif(!preg_match('/^080/', $number) and !preg_match('/^070/', $number) and !preg_match('/^090/', $number) and !preg_match('/^081/', $number)) {
return 110;
}

//Check if the length is 11 digits
elseif(strlen($number)!==11) {
return 120;
}

//Every requirements are made
else {
return 200;
}
}
?>

Use this function like:
<?php
echo check_number(08179751185);
?>

Email Newsletter



Smiley :)
:D
:)
:[
;)
:D
:O
(6)
(A)
:'(
:|
:o)
8)
(K)
(M)

11 comments:

  1. Wow, its a nice code u've written. I tro my cap

    ReplyDelete
  2. Hi,
    Please hold back your cap :D
    Its just a piece of code :)

    ReplyDelete
  3. this is neat enough but... when Checking if number starts with 080, 090, 070 and 081.... Shouldnt it be OR and not AND.

    ReplyDelete
  4. Hi Durex,
    Sometimes I usually get confuse on which to use, I must try both before I know which gives me what I want ;)
    But this works quite okay :)

    ReplyDelete
  5. Nice code you have written, take note of these few exceptions to handle
    1. Some networks provide 12 digit numbers e.g 080663728100
    2. More also consider Nigerian phone code extensions e.g
    0092348066372810 is a Nigerian number but would not be verified by your code.
    0092348066372810 => 009 + 234 + 08066372810

    This additions can be included

    ReplyDelete
  6. Hi Kelechi,

    Thanks for the compliments :)
    Actually I haven't seen a mobile phone number greater than or less than 11 digits, for the country code I suggest after validation of the phone number then you can use substr() function and strip off the first "0" and add +234 or 009.

    How's that? :)

    ReplyDelete
  7. yea, i neva checked well. When doing d preg_match, it shuld be or and not and.

    ReplyDelete
  8. Nah bro, its "and", now let's analize that :)
    When we use "or" it will check the first match (!preg_match('/^080/', $number)) and never checks the second match but "and" checks all to see if it meets the requirements. You should test this in your server :)

    And I made some modifications in the regex changing "/080/" to "/^080/". The old one checks if it exists in a string but the new one checks if its starting :)

    ReplyDelete
  9. Nice code. "and" is the right join condition. Also


    using "08179751185" will parse as string.

    ReplyDelete
  10. Hi Ulo,
    Thanks for the compliment, parsing a string will still work right?

    ReplyDelete
  11. thanks bro, nice piece of code you crafted there, keep it up

    ReplyDelete