Best practice for parsing and validating mobile number

后端 未结 4 671
说谎
说谎 2020-12-31 17:43

I wonder what the best practice for parsing and validating a mobile number before sending a text is. I\'ve got code that works, but I\'d like to find out better ways of doin

相关标签:
4条回答
  • 2020-12-31 17:50

    Check out QAS, it's a commercial solution.

    They have email, phone and address validations.

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

    We use their services for Address and Email (not phone) and have been satisfied with it.

    0 讨论(0)
  • 2020-12-31 17:52

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

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

    Alternatively, I would recommend you use a masked textbox to collect the # (there are many options available) to allow only numeric input, and display the input with whatever format you'd like. This way you're guaranteeing that the value received will be all numeric characters.

    0 讨论(0)
  • 2020-12-31 17:52
    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 讨论(0)
  • 2020-12-31 18:11

    @annelie maybe you can update your regular expression to a more powerful one. Check out this site here. It contains many expressions but I think one of the top 2 expressions in the site should be suitable to you.

    0 讨论(0)
提交回复
热议问题