Validating South African Cell Numbers with a Regular Expression (Regex)

These days legislation increasingly places an obligation on processors of personal information to ensure that information is accurate.  Input sanitisation and data validation are critical approaches to maintaining accuracy.

I scrounged information about active South African mobile number prefixes using information from the Wikipedia article on South African telephone numbers, a February 2016 post on the Asterisk.org.za mailing list, as well as HLR validation logs.

After munging all of the info from those sources, the following regular expression – as at today – validates all cellphone numbers using known active prefixes in South Africa and excludes the rest.

I took care to try and ensure the regex is as character compact as possible while still providing for all known 3 and 4 digit prefixes.

The regex is:

0((60[3-9]|64[0-5])\d{6}|(7[1-4689]|6[1-3]|8[1-4])\d{7})

An example usage (including start and ending terminators) would be:

SELECT * FROM cellnumber_listing
WHERE NOT (trim(celltelephone) ~ '^0((60[3-9]|64[0-5])\d{6}|(7[1-4689]|6[1-3]|8[1-4])\d{7})$');

Or for Perl Compatible Regular Expression (PCRE):

grep -P '27((60[3-9]|64[0-5])\d{6}|(7[1-4689]|6[1-3]|8[1-4])\d{7})' file.txt

If you have any comments on the above or suggestions how it could be improved, please comment!

Be the first to comment

Leave a Reply

Your email address will not be published.


*