How to validate a (country specific) phone number

前端 未结 8 700
一个人的身影
一个人的身影 2020-12-30 00:44

A valid phone number contains:

  • Less than 9 characters
  • A "+" at the start
  • Only digits.

I\'m trying to use regular expre

8条回答
  •  不知归路
    2020-12-30 00:55

    DON'T USE A REGULAR EXPRESSION!!

    There are too many variables for a regex to be of any use. Instead, just remove all characters from your string that are not 0-9, and then check to see if you have the correct number of digits left. Then it doesn't matter what extra stuff the user includes or doesn't include... ()x-+[] etc etc, as it just strips them all out and only counts the characters 0-9.

    I've got a string extension that works great, and allows for a wide range of formats. It accepts an IsRequired parameter. So, you can validate a phone number like this:

    string phone = "(999)999-9999"
    bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true
    
    string phone ="1234567890"
    bool isValidPhone = phone.ValidatePhoneNumber(true) // returns true
    
    string phone = ""
    bool isValidPhone = phone.ValidatePhoneNumber(false) // not required, so returns true
    
    string phone = ""
    bool isValidPhone = phone.ValidatePhoneNumber(true) // required, so returns false
    
    string phone ="12345"
    bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false
    
    string phone ="foobar"
    bool isValidPhone = phone.ValidatePhoneNumber(true) // returns false
    

    Here's the code (assumes a 10-digit American phone number. Adjust accordingly):

    public static class StringExtensions
    {
    
        /// 
        /// Checks to be sure a phone number contains 10 digits as per American phone numbers.  
        /// If 'IsRequired' is true, then an empty string will return False. 
        /// If 'IsRequired' is false, then an empty string will return True.
        /// 
        /// 
        /// 
        /// 
        public static bool ValidatePhoneNumber(this string phone, bool IsRequired)
        {
            if (string.IsNullOrEmpty(phone) & !IsRequired)
                return true;
    
            if (string.IsNullOrEmpty(phone) & IsRequired)
                return false;
    
            var cleaned = phone.RemoveNonNumeric();
            if (IsRequired)
            {
                if (cleaned.Length == 10)
                    return true;
                else
                    return false;
            }
            else
            {
                if (cleaned.Length == 0)
                    return true;
                else if (cleaned.Length > 0 & cleaned.Length < 10)
                    return false;
                else if (cleaned.Length == 10)
                    return true;
                else
                    return false; // should never get here
            }
        }
    
        /// 
        /// Removes all non numeric characters from a string
        /// 
        /// 
        /// 
        public static string RemoveNonNumeric(this string phone)
        {
            return Regex.Replace(phone, @"[^0-9]+", "");
        }
    }
    

提交回复
热议问题