Best practice for parsing and checking mobile number

I wonder what is the best practice for parsing and checking a mobile number before sending text. I have code that works, but I would like to find more efficient ways to execute it (as my last question, this is part of my early resolution for new years to write better quality code!).

At the moment, we really forgive when a user enters a number in the form, they can enter things like "+44 123 4567890", "00441234567890", "0123456789", "+44 (0) 123456789", 012-345-6789 "or even" did not receive the phone. "

However, to send text, the format must be 44xxxxxxxxxx (this is only for UK mobile phones), so we need to parse it and check it before we can send it. Below is the code that I have at the moment (C #, asp.net), it would be great if anyone had ideas on how to improve it.

Thanks,

Anneli

private bool IsMobileNumberValid(string mobileNumber) { // parse the number _mobileNumber = ParsedMobileNumber(mobileNumber); // check if it the right length if (_mobileNumber.Length != 12) { return false; } // check if it contains non-numeric characters if(!Regex.IsMatch(_mobileNumber, @"^[-+]?[0-9]*\.?[0-9]+$")) { return false; } return true; } private string ParsedMobileNumber(string number) { number = number.Replace("+", ""); number = number.Replace(".", ""); number = number.Replace(" ", ""); number = number.Replace("-", ""); number = number.Replace("/", ""); number = number.Replace("(", ""); number = number.Replace(")", ""); number = number.Trim(new char[] { '0' }); if (!number.StartsWith("44")) { number = "44" + number; } return number; } 

EDIT

Here is what I ended up with:

 private bool IsMobileNumberValid(string mobileNumber) { // remove all non-numeric characters _mobileNumber = CleanNumber(mobileNumber); // trim any leading zeros _mobileNumber = _mobileNumber.TrimStart(new char[] { '0' }); // check for this in case they've entered 44 (0)xxxxxxxxx or similar if (_mobileNumber.StartsWith("440")) { _mobileNumber = _mobileNumber.Remove(2, 1); } // add country code if they haven't entered it if (!_mobileNumber.StartsWith("44")) { _mobileNumber = "44" + _mobileNumber; } // check if it the right length if (_mobileNumber.Length != 12) { return false; } return true; } private string CleanNumber(string phone) { Regex digitsOnly = new Regex(@"[^\d]"); return digitsOnly.Replace(phone, ""); } 
+7
source share
4 answers

Use a regular expression to remove any non-numeric characters instead of trying to guess how the person will enter their number - this will delete all your Replace () and Trim () methods if you really don't need to trim the leading zero.

 string CleanPhone(string phone) { Regex digitsOnly = new Regex(@"[^\d]"); return digitsOnly.Replace(phone, ""); } 

As an alternative, I would recommend that you use a masked text field to collect # (there are many options available) to allow only numeric input and display the input in any format you need. Thus, you guarantee that the resulting value will consist of all digital characters.

+2
source

Check out QAS, this is a commercial solution.

They have confirmation by email, phone and address.

http://www.qas.com/phone-number-validation-web-service.htm

We use their services for the address and email (and not for the phone) and are satisfied with it.

+1
source

@annelie, maybe you can upgrade your regex to a more powerful one. Browse this site here . It contains a lot of expressions, but I think one of the two best expressions on the site should be right for you.

0
source
 public class PhoneNumber { public PhoneNumber(string value) { if (String.IsNullOrEmpty(value)) throw new ArgumentNullException("numberString", Properties.Resources.PhoneNumberIsNullOrEmpty); var match = new Regex(@"\+(\w+) \((\w+)\) (\w+)", RegexOptions.Compiled).Match(value); if (match.Success) { ushort countryCode = 0; ushort localCode = 0; int number = 0; if (UInt16.TryParse(match.Result("$1"), out countryCode) && UInt16.TryParse(match.Result("$2"), out localCode) && Int32.TryParse(match.Result("$3"), out number)) { this.CountryCode = countryCode; this.LocalCode = localCode; this.Number = number; } } else { throw new ArgumentNullException("numberString", Properties.Resources.PhoneNumberInvalid); } } public PhoneNumber(int countryCode, int localCode, int number) { if (countryCode == 0) throw new ArgumentOutOfRangeException("countryCode", Properties.Resources.PhoneNumberIsNullOrEmpty); else if (localCode == 0) throw new ArgumentOutOfRangeException("localCode", Properties.Resources.PhoneNumberIsNullOrEmpty); else if (number == 0) throw new ArgumentOutOfRangeException("number", Properties.Resources.PhoneNumberIsNullOrEmpty); this.CountryCode = countryCode; this.LocalCode = localCode; this.Number = number; } public int CountryCode { get; set; } public int LocalCode { get; set; } public int Number { get; set; } public override string ToString() { return String.Format(System.Globalization.CultureInfo.CurrentCulture, "+{0} ({1}) {2}", CountryCode, LocalCode, Number); } public static bool Validate(string value) { return new Regex(@"\+\w+ \(\w+\) \w+", RegexOptions.Compiled).IsMatch(value); } public static bool Validate(string countryCode, string localCode, string number, out PhoneNumber phoneNumber) { var valid = false; phoneNumber = null; try { ushort uCountryCode = 0; ushort uLocalCode = 0; int iNumber = 0; // match only if all three numbers have been parsed successfully valid = UInt16.TryParse(countryCode, out uCountryCode) && UInt16.TryParse(localCode, out uLocalCode) && Int32.TryParse(number, out iNumber); if (valid) phoneNumber = new PhoneNumber(uCountryCode, uLocalCode, iNumber); } catch (ArgumentException) { // still not match } return valid; } } 
0
source

All Articles