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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?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:
1 2 3 |
<?php echo check_number('08179751185'); ?> |