Thursday, August 26, 2010

Phone Validation

//class represent the Rules Violation

    #region
  
    public class RuleViolation
    {
        public string ErrorMessage
        {
            get;
            private set;
        }
        public string PropertyName
        {
            get;
            private set;
        }

        public RuleViolation(string errorMessage)
        {
            ErrorMessage = errorMessage;
        }
        public RuleViolation(string errorMessage, string propertyName)
        {
            ErrorMessage = errorMessage;
            PropertyName = propertyName;
        }
    }

//-------------------------------------------------//
//class represent the phone validation

    #region
    public class PhoneValidator
    {
        static IDictionary countryRegx = new Dictionary()
        {
            {"IN",new Regex("^[0-9]+$")},
            {"USA",new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
            {"Netherlands",new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")}
        };
        public static bool IsValidNumber(string phoneNumber, string country)
        {
            if (country != null && countryRegx.ContainsKey(country))
                return countryRegx[country].IsMatch(phoneNumber);
            else
                return false;
        }
        public static IEnumerable Countries
        {
            get { return countryRegx.Keys; }
        }
    }