Exemplo n.º 1
0
        private static string RemoveExtension(string potentialPhoneNumber)
        {
            int length1 = potentialPhoneNumber.LastIndexOf("ext.", StringComparison.InvariantCultureIgnoreCase);

            if (length1 >= 0 && PhoneNumberAttribute.MatchesExtension(potentialPhoneNumber.Substring(length1 + 4)))
            {
                return(potentialPhoneNumber.Substring(0, length1));
            }
            int length2 = potentialPhoneNumber.LastIndexOf("ext", StringComparison.InvariantCultureIgnoreCase);

            if (length2 >= 0 && PhoneNumberAttribute.MatchesExtension(potentialPhoneNumber.Substring(length2 + 3)))
            {
                return(potentialPhoneNumber.Substring(0, length2));
            }
            int length3 = potentialPhoneNumber.LastIndexOf("x", StringComparison.InvariantCultureIgnoreCase);

            return(length3 >= 0 && PhoneNumberAttribute.MatchesExtension(potentialPhoneNumber.Substring(length3 + 1)) ? potentialPhoneNumber.Substring(0, length3) : potentialPhoneNumber);
        }
Exemplo n.º 2
0
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return(true);
            }
            string input = value as string;

            if (input == string.Empty)
            {
                return(true);
            }
            if (PhoneNumberAttribute._regex != null)
            {
                return(input != null && PhoneNumberAttribute._regex.Match(input).Length > 0);
            }
            if (input == null)
            {
                return(false);
            }
            string str  = PhoneNumberAttribute.RemoveExtension(input.Replace("+", string.Empty).TrimEnd());
            bool   flag = false;

            foreach (char c in str)
            {
                if (char.IsDigit(c))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                return(false);
            }
            foreach (char c in str)
            {
                if (!char.IsDigit(c) && !char.IsWhiteSpace(c) && "-.()".IndexOf(c) == -1)
                {
                    return(false);
                }
            }
            return(true);
        }