Verify South Africa Cell Phone Number

How can I verify a cell phone number in South Africa? The number should begin with a country code, such as +27, and be no more than 11 digits - only 12 with a "+" sign.

+6
php
source share
5 answers

Try regex, for example:

^\+27[0-9]{9}$ 

PHP translation that will look like this:

 if( preg_match( "/^\+27[0-9]{9}$/", $phoneNumber ) ){ echo "Valid number"; } else { echo "Invalid number"; } 
+6
source share

Take the entrance and cross out everything that is not a number.

 // rm all but Numbers $input = '0123 abc #'; $output = preg_replace('#[^0-9]#', '', $input); echo($output); 

Count the remaining numbers.

If it's 9 digits, add "+27"

If it's 11 digits, add "+"

If it is 10 digits longer or less than 9, then presumably this is not a valid phone number format?

+1
source share
 $number = preg_replace('/[^0-9]/', '', $number); if ((substr($number, 0, 2) != '27') || strlen($number) != 11) { return false; } // else valid 
0
source share

The best use of Regex is 0((60[3-9]|64[0-5]|66[0-5])\d{6}|(7[1-4689]|6[1-3]|8[1-4])\d{7})

Regex Regulator - https://www.regextester.com/?fam=105448

a source

0
source share

The OP was for a South African mobile phone (mobile), not just a South African number.

The following regular expression worked for me:

 ^(\+27|0)[6-8][0-9]{8}$ 
  • Allow international dialing code or start from scratch.
  • South African phone numbers start at 08 or 07 or 06.
0
source share

All Articles