I created this code that i think will be of big value for people working with mobile numbers in South Africa.
I created regular expressions to validate it.
On your page just include the following reference
using System.Text.RegularExpressions;
then i created the following two methods.
This one will strip all non Numeric characters from the string you pass in and then convert it to international format...
public static string StripNumber(string p_Number) { try { string cleanNumber = ""; //Strip all the invalid chars. for (int inum = 0; inum < p_Number.Length; inum++) { try { if (Convert.ToInt16(p_Number.Substring(inum, 1)) >= 0 && Convert.ToInt16(p_Number.Substring(inum, 1)) <= 9) { cleanNumber += p_Number.Substring(inum, 1); }
} catch {
}
} //make sure that the number gets converted to international Number format excluding + if (cleanNumber.StartsWith("0")) { return "27" + cleanNumber.Substring(1, cleanNumber.Length - 1);
} else if (cleanNumber.StartsWith("27")) { return cleanNumber; } else { MessageBox.Show("Please check the validation of this number : " + cleanNumber); return cleanNumber; } } catch (Exception ex) { return ""; } }
Now here is the big one :-) it takes your number that you pass in, call the stripNumber and then goes to the regular expression part and validates the string
public static Boolean ValidateNumber(string p_Number) { try { //International format as per Email from MTN (RDV created expression) /* MTN: 27710,27717,27718,27719,2773,2778,2783 VC: 27711,27712,27713,27714,27715,27716,2772,2776,2779,2782 CellC: 2784,2774 8ta: 2781 Regular Expression for 07X numbers (International) 277[1-4|6|8-9][0-9]{7} Regular Expression for 08X numbers (International) 278[1-4][0-9]{7} Regular Expression for 07X numbers (International) 07[1-4|6|8-9][0-9]{7} Regular Expression for 08X numbers (International) 08[1-4][0-9]{7}
*/ string CleanNumber = StripNumber(p_Number); //is it a 07x number International? if (Regex.IsMatch(CleanNumber, @"277[1-4|6|8-9][0-9]{7}") == true) { return true; } //Is it a 08X number International? if (Regex.IsMatch(CleanNumber, @"278[1-4][0-9]{7}") == true) { return true; }
return false;
} catch (Exception ex) {
return false; } finally {
}
}
Ruan de Villiers
UTi Mounties
MCTS - ASP.net 3.5
Gtalk - Ruandv@gmail.com
__________________________________________________________________
Please Mark as Answered If post is helpful.
Gremlin1708
Member
500 Points
141 Posts
Validate mobile numbers based in South Africa.
Jun 24, 2011 10:28 AM|LINK
Hi everyone..
I created this code that i think will be of big value for people working with mobile numbers in South Africa.
I created regular expressions to validate it.
On your page just include the following reference
then i created the following two methods.
This one will strip all non Numeric characters from the string you pass in and then convert it to international format...
Now here is the big one :-) it takes your number that you pass in, call the stripNumber and then goes to the regular expression part and validates the string
UTi Mounties
MCTS - ASP.net 3.5
Gtalk - Ruandv@gmail.com
__________________________________________________________________
Please Mark as Answered If post is helpful.