Ejemplo n.º 1
0
        /// <summary>
        /// Validates the specified element.
        /// </summary>
        /// <param name="modelState">State of the model.</param>
        /// <param name="name">The element name.</param>
        /// <param name="value">The element value.</param>
        public void Validate(ModelStateDictionary modelState, string name, string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                return;
            }

            if (!Regex.IsMatch(value, RegexValidationConfig.GetPattern(RegexTemplates.Email)))
            {
                modelState.AddModelError(name, @"The field format is not valid.");
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Validates the article model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="modelState">State of the model.</param>
 public static void ValidateArticle(ArticleViewModel model, ModelStateDictionary modelState)
 {
     if (!String.IsNullOrEmpty(model.Url))
     {
         if (model.UrlType == ArticleUrlType.External)
         {
             if (!Regex.IsMatch(model.Url, RegexValidationConfig.GetPattern(RegexTemplates.Url)))
             {
                 modelState.AddModelError(PropertyName.For <ArticleViewModel>(item => item.Url),
                                          ResourceHelper.TranslateErrorMessage(new HttpContextWrapper(HttpContext.Current), typeof(ArticleViewModel),
                                                                               PropertyName.For <ArticleViewModel>(item => item.Url), "regularexpression", null));
             }
         }
         else if (model.UrlType == ArticleUrlType.Internal)
         {
             if (!Regex.IsMatch(model.Url, RegexValidationConfig.GetPattern(RegexTemplates.UrlPart)))
             {
                 modelState.AddModelError(PropertyName.For <ArticleViewModel>(item => item.Url),
                                          ResourceHelper.TranslateErrorMessage(new HttpContextWrapper(HttpContext.Current), typeof(ArticleViewModel),
                                                                               PropertyName.For <ArticleViewModel>(item => item.Url), "regularexpression", null));
             }
         }
     }
 }