How to validate a (country specific) phone number

前端 未结 8 703
一个人的身影
一个人的身影 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:52

    Expanding upon one of the answers provided above, the method I came up with to also handle a few phone number delivery styles as well as international phone number is

        internal static bool IsValidPhoneNumber(this string This)
        {
            var phoneNumber = This.Trim()
                .Replace(" ", "")
                .Replace("-", "")
                .Replace("(", "")
                .Replace(")", "");
            return Regex.Match(phoneNumber, @"^\+\d{5,15}$").Success;
        }
    

提交回复
热议问题