Beispiel #1
0
        /// <summary>
        /// 验证文本框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="option">验证规则</param>
        /// <returns></returns>
        public static MvcHtmlString TextBoxExFor <TModel, TProperty>(
            this HtmlHelper <TModel> htmlHelper,
            Expression <Func <TModel, TProperty> > expression,
            object htmlAttributes,
            ValidateOption option = null
            )
        {
            // 拼装验证属性
            var tagerHtmlAtt = ((IDictionary <string, object>) new RouteValueDictionary(htmlAttributes));

            // 默认描述
            if (!string.IsNullOrEmpty(option.Description))
            {
                tagerHtmlAtt.Add("description", option.Description);
            }
            // 失去焦点描述
            if (!string.IsNullOrEmpty(option.FocusMsg))
            {
                tagerHtmlAtt.Add("focusMsg", option.FocusMsg);
            }
            // 范围验证
            if (option.Min != null)
            {
                tagerHtmlAtt.Add("min", option.Min);
                if (!string.IsNullOrEmpty(option.RangeMsg))
                {
                    tagerHtmlAtt.Add("rangeMsg", option.RangeMsg);
                }
            }


            // 非空验证
            if (option.IsEmpty)
            {
                tagerHtmlAtt.Add("isEmpty", "true");
            }
            else
            {
                if (!string.IsNullOrEmpty(option.EmptyMsg))
                {
                    tagerHtmlAtt.Add("emptyMsg", option.EmptyMsg);
                }
                tagerHtmlAtt.Add("isEmpty", "false");
            }


            //正则验证
            if (!string.IsNullOrEmpty(option.Regex))
            {
                tagerHtmlAtt.Add("regex", option.Regex);
                tagerHtmlAtt.Add("regexMsg", option.RegexMsg);
            }

            if (option.Max != null)
            {
                tagerHtmlAtt.Add("max", option.Max);
                if (option.RangeMsg != null && tagerHtmlAtt.ContainsKey(option.RangeMsg))
                {
                    tagerHtmlAtt.Add("rangeMsg", option.RangeMsg);
                }
            }
            if (option.AjaxUrl != null)
            {
                tagerHtmlAtt.Add("ajax", option.AjaxUrl);
            }
            if (option.CompareElem != null)
            {
                tagerHtmlAtt.Add("compareElemId", option.CompareElem);
                if (!string.IsNullOrEmpty(option.CompareElemMsg))
                {
                    tagerHtmlAtt.Add("compareElemMsg", option.CompareElemMsg);
                }
            }



            tagerHtmlAtt.Add("validator", "true");
            return(htmlHelper.TextBoxFor(expression, tagerHtmlAtt));
        }
Beispiel #2
0
        /// <summary>
        /// 验证文本框
        /// </summary>
        /// <param name="name">验证文本框Name</param>
        /// <param name="htmlAttributes"></param>
        /// <param name="option">验证规则</param>
        /// <returns></returns>
        public static MvcHtmlString TextBoxEx(
            this HtmlHelper htmlHelper,
            string name,
            object htmlAttributes,
            ValidateOption option = null
            )
        {
            // 拼装验证属性
            var attrs = ((IDictionary <string, object>) new RouteValueDictionary(htmlAttributes));



            // 默认描述
            if (!string.IsNullOrEmpty(option.Description))
            {
                attrs.Add("description", option.Description);
            }
            // 失去焦点描述
            if (!string.IsNullOrEmpty(option.FocusMsg))
            {
                attrs.Add("focusMsg", option.FocusMsg);
            }
            // 范围验证
            if (option.Min != null)
            {
                attrs.Add("min", option.Min);
                if (!string.IsNullOrEmpty(option.RangeMsg))
                {
                    attrs.Add("rangeMsg", option.RangeMsg);
                }
            }

            // 非空验证
            if (option.IsEmpty)
            {
                attrs.Add("isEmpty", "true");
            }
            else
            {
                if (!string.IsNullOrEmpty(option.EmptyMsg))
                {
                    attrs.Add("emptyMsg", option.EmptyMsg);
                }
                attrs.Add("isEmpty", "false");
            }


            //正则验证
            if (!string.IsNullOrEmpty(option.Regex))
            {
                attrs.Add("regex", option.Regex);
                attrs.Add("regexMsg", option.RegexMsg);
            }


            if (option.Max != null)
            {
                attrs.Add("max", option.Max);
                if (option.RangeMsg != null && attrs.ContainsKey(option.RangeMsg))
                {
                    attrs.Add("rangeMsg", option.RangeMsg);
                }
            }
            if (option.AjaxUrl != null)
            {
                attrs.Add("ajax", option.AjaxUrl);
            }
            if (option.CompareElem != null)
            {
                attrs.Add("compareElemId", option.CompareElem);
                if (!string.IsNullOrEmpty(option.CompareElemMsg))
                {
                    attrs.Add("compareElemMsg", option.CompareElemMsg);
                }
            }

            attrs.Add("validator", "true");
            return(htmlHelper.TextBox(name, null, attrs));
        }