Beispiel #1
0
        public static bool RegexIsMatch(object target, ValidationRuleArgs e)
        {
            RegexRuleArgs args = e as RegexRuleArgs;

            if (args == null)
            {
                throw new ArgumentException("Invalid ValidationRuleArgs.  e must be of type RegexRuleArgs.");
            }
            string       pattern  = args.Expression;
            PropertyInfo property = target.GetType().GetProperty(e.PropertyName);

            if (property == null)
            {
                throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
            }
            if (property.PropertyType != typeof(string))
            {
                throw new ArgumentException(string.Format("Property \"{0}\" is not of type String.", e.PropertyName));
            }
            string input = (string)property.GetValue(target, null);

            if ((input == null) || !Regex.IsMatch(input, pattern))
            {
                if (string.IsNullOrEmpty(e.Description))
                {
                    e.Description = string.Format("{0} do not match the regular expression {1}", e.PropertyName, pattern);
                }
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Rule ensuring a String value is matching
        /// a specified regular expression.
        /// </summary>
        /// <param name="target">Object containing the data to validate.</param>
        /// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated, must be of type RegexRuleArgs</param>
        /// <returns>False if the rule is broken; true otherwise.</returns>
        /// <remarks>
        /// This implementation uses late binding, and will only work
        /// against String property values.
        /// </remarks>
        public static bool RegexIsMatch(object target, ValidationRuleArgs e)
        {
            RegexRuleArgs args = e as RegexRuleArgs;

            if (args != null)
            {
                string expression = args.Expression;

                PropertyInfo p = target.GetType().GetProperty(e.PropertyName);

                if (p != null)
                {
                    if (p.PropertyType == typeof(string))
                    {
                        string value = (string)p.GetValue(target, null);

                        if (value == null || !Regex.IsMatch(value, expression))
                        {
                            if (string.IsNullOrEmpty(e.Description))
                            {
                                e.Description = String.Format("{0} is not valid.", e.FriendlyName);
                            }
                            return(false);
                        }
                        return(true);
                    }
                    else
                    {
                        throw new ArgumentException(string.Format("Property \"{0}\" is not of type String.", e.PropertyName));
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
                }
            }
            else
            {
                throw new ArgumentException("Invalid ValidationRuleArgs. e must be of type RegexRuleArgs.");
            }
        }