开发者

Validate South Africa Cell Phone Number

How can I validate a South African cell phone number? The number must star开发者_开发百科t with the country code like +27 and be max 11 digits long - total 12 with the "+" sign.


The OP was for a South African cell phone (mobile) number not just any South African telephone number.

The following regex is what worked for me:

^(\+27|0)[6-8][0-9]{8}$
  • Allow for international dialling code or starting with zero.
  • South African cell numbers start with 08 or 07 or 06.


Try a regular expression like:

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

Translated to PHP that would be:

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


Take the input and strip out everything which is not a number.

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

Count the remaining digits.

If it is 9 digits long, prepend "+27"

If it is 11 digits long prepend a "+"

If it is 10 digits long or less than 9, then presumably it is not a valid tel number format?


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

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

source


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜