Exemple #1
0
        public static void EqualTo(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            bool result = false;

            if (param.Params != null && param.Params.ContainsKey(ValidationMethodology.EqualTo))
            {
                List <bool> objParamResults = new List <bool>();

                for (int i = 0; i < param.Params.Count(); i++)
                {
                    object obj = param.Params[param.Params.ElementAt(i).Key];

                    if (obj is List <object> )
                    {
                        List <object> objParams = (List <object>)obj;
                        foreach (object objParam in objParams)
                        {
                            objParamResults.Add(Convert.ToString(param.Value) == Convert.ToString(objParam));
                        }
                        result = (objParamResults.Count == 0 || objParamResults.Contains(true));
                    }
                    else
                    {
                        result = Convert.ToString(param.Value) == Convert.ToString(obj);
                    }
                }
            }

            if (!result && string.IsNullOrEmpty(param.ErrorMessage))
            {
                param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? "Please enter same value" : Message;
                MSD.AddModelError(param.PropertyName, param.ErrorMessage);
            }
        }
Exemple #2
0
 public static void AlphaNumericOnly(ValidationParam param, ModelStateDictionary MSD, string Message)
 {
     if (param.Value != null && Convert.ToString(param.Value).ToCharArray().All(c => Char.IsLetter(c) || Char.IsNumber(c)))
     {
         if (string.IsNullOrEmpty(param.ErrorMessage))
         {
             param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? "Please enter alphanumerics only" : Message;
             MSD.AddModelError(param.PropertyName, param.ErrorMessage);
         }
     }
 }
Exemple #3
0
        public static void Url(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            if (param.Value != null && !Uri.IsWellFormedUriString(Convert.ToString(param.Value), UriKind.RelativeOrAbsolute))
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? string.Format("Please enter valid url") : Message;

                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }
            }
        }
Exemple #4
0
        public static void NumericOnly(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            long result;

            if (!long.TryParse(Convert.ToString(param.Value), out result))
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? "Please enter numbers only" : Message;
                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }
            }
        }
Exemple #5
0
        /*Parameters @"(\w+)\s+(car)";*/
        public static void RegularExpression(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            dynamic regularExpressionParam = null;

            if (!(param.Value != null && param.Params != null && param.Params.TryGetValue(ValidationMethodology.RegularExpression, out regularExpressionParam) && regularExpressionParam != null && new Regex(regularExpressionParam).IsMatch(Convert.ToString(param.Value))))
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? string.Format("Please enter valid input") : Message;

                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }
            }
        }
Exemple #6
0
        /*Parameters new { Top = 10; Bottom = 6};*/
        public static void RangeLength(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            dynamic lengthParam = null;

            if (!(param.Value != null && param.Params != null && param.Params.TryGetValue(ValidationMethodology.Range, out lengthParam) && lengthParam.Bottom <= Convert.ToString(param.Value).Length <= lengthParam.Top))
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? string.Format("Please enter value of length between {0} and {1} ", Convert.ToString(lengthParam.Bottom), Convert.ToString(lengthParam.Top)) : Message;

                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }
            }
        }
Exemple #7
0
        /*Parameters 10;*/
        public static void MaxLength(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            dynamic lengthParam = null;

            if (!(param.Value != null && param.Params != null && param.Params.TryGetValue(ValidationMethodology.MaxLength, out lengthParam) && Convert.ToString(param.Value).Length <= lengthParam))
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? string.Format("Please enter value with maximum length of {0} ", Convert.ToString(lengthParam)) : Message;

                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }
            }
        }
Exemple #8
0
        public static void GreaterThan(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            dynamic comparisionParam = null;

            if (!(param.Value != null && param.Params != null && param.Params.TryGetValue(ValidationMethodology.GreaterThan, out comparisionParam) && Convert.ToInt64(param.Value) > Convert.ToInt64(comparisionParam)))
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? "Please enter value greater than " + Convert.ToString(comparisionParam) : Message;

                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }
            }
        }
Exemple #9
0
        public static bool Email(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            try
            {
                MailAddress m = new MailAddress(Convert.ToString(param.Value));

                return(true);
            }
            catch (Exception)
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? "Please enter valid email address" : Message;
                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }

                return(false);
            }
        }
Exemple #10
0
        public static bool Date(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            DateTime dt;

            object format = string.Empty;

            bool result = param.Value != null &&
                          param.Params != null &&
                          param.Params.Count > 0 &&
                          param.Params.TryGetValue(ValidationMethodology.Date, out format) &&
                          format != null && DateTime.TryParseExact(param.Value.ToString(), format.ToString(), new CultureInfo("en-US"), DateTimeStyles.None, out dt);


            if (!result && string.IsNullOrEmpty(param.ErrorMessage))
            {
                param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? "Please enter valid datetime" : Message;
                MSD.AddModelError(param.PropertyName, param.ErrorMessage);
            }

            return(result);
        }
Exemple #11
0
        public static bool Required(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            bool result = false;

            if (param.Params != null && param.Params.ContainsKey(ValidationMethodology.Required))
            {
                List <bool> objParamResults = new List <bool>();

                for (int i = 0; i < param.Params.Count(); i++)
                {
                    object obj = param.Params[param.Params.ElementAt(i).Key];

                    if (obj is List <object> )
                    {
                        List <object> objParams = (List <object>)obj;
                        foreach (object objParam in objParams)
                        {
                            objParamResults.Add(param.Value != objParam);
                        }
                    }
                }

                result = (objParamResults.Count == 0 || objParamResults.Contains(false));
            }
            else
            {
                result = GetDefault(param.Type) != param.Value; //param != null && (param.Type.GetConstructor(new Type[] { }).Invoke(new object[] { }) != param.Value);
            }

            if (!result)
            {
                param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? "This field is required" : Message;

                MSD.AddModelError(param.PropertyName, param.ErrorMessage);
            }

            return(result);
        }
Exemple #12
0
        public static void FileExtension(ValidationParam param, ModelStateDictionary MSD, string Message)
        {
            dynamic fileParam = null;

            bool result = (param.Value != null && param.Params != null && param.Params.TryGetValue(ValidationMethodology.FileExtension, out fileParam) && fileParam != null && fileParam.Length > 0);

            if (result)
            {
                string extension = Path.GetExtension((param.Value as HttpPostedFileBase).FileName);

                result = (fileParam as string[]).OfType <string>().ToList().Contains(extension.Trim('.'));
            }

            if (!result)
            {
                if (string.IsNullOrEmpty(param.ErrorMessage))
                {
                    param.ErrorMessage = (string.IsNullOrEmpty(Message)) ? string.Format("Please select valid file") : Message;

                    MSD.AddModelError(param.PropertyName, param.ErrorMessage);
                }
            }
        }