Validate SA Cell Number with Javascript

function validate(){
var MobileNumber = document.getElementById(“MobileNumber”).value;
var MobileNumberRegEx = /^\+27[78][23467][0-9]{7}/;
if (MobileNumberRegEx.test(MobileNumber)) {
return true;
}else{
alert(“Please enter a valid South African mobile phone number\nin the following format: +27820000000”);
return false;
}
}

Advertisements

isEmail

The following C# function returns true if the supplied inputEmail is a valid email address.

private static bool isEmail(string inputEmail)
{
  inputEmail =
NulltoString(inputEmail);
  string strRegex = @”^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}” +
  @”\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\” +
  @”.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$”;
  Regex re = new Regex(strRegex);
  if (re.IsMatch(inputEmail))
  return (true);
  else
  return (false);
}

isMobile

This function returns true if the string provided is a valid South African mobile phone number in the following format: 27821234567.

private static bool isMobile(string inputMobile)
{
    inputMobile =
NulltoString(inputMobile);
    string strRegex = “(^27[78][2347][0-9]{7})”;
    Regex re = new Regex(strRegex);
    if (re.IsMatch(inputMobile))
        return (true);
    else
        return (false);
}

Regular Expression Library

RegExLib.com has a great collection of regular expressions. Developing for South African users, I find this collection particularly usefull.