コード例 #1
0
        /// <summary>
        /// 验证方法
        /// </summary>
        /// <param name="value">需要验证的值</param>
        /// <returns>返回验证结果</returns>
        public ResultValidation Validation(object value)
        {
            //判断是否设置了异常提示信息,如果没有,设置默认值
            if (string.IsNullOrEmpty(this.ErrorMessage))
            {
                this.ErrorMessage = "字段长度不能小于{0}或大于{1}";
            }

            ResultValidation ev = new ResultValidation()
            {
                IsSuccess = true
            };

            if (value != null)
            {
                int newLength = value.ToString().Length;
                if (newLength >= MinLength && newLength <= MaxLength)
                {
                    return(ev);
                }
                else
                {
                    ev.ErrorMessage = string.Format(this.ErrorMessage, this.MinLength, this.MaxLength);
                    ev.IsSuccess    = false;
                }
            }
            else
            {
                ev.ErrorMessage = this.ErrorMessage;
                ev.IsSuccess    = false;
            }
            return(ev);;
        }
コード例 #2
0
        /// <summary>
        /// 验证方法
        /// </summary>
        /// <param name="value">需要验证的值</param>
        /// <returns>返回验证结果</returns>
        public ResultValidation Validation(object value)
        {
            //判断是否设置了异常提示信息,如果没有,设置默认值
            if (string.IsNullOrEmpty(this.ErrorMessage))
            {
                this.ErrorMessage = "字段不能小于{0}或大于{1}";
            }

            ResultValidation ev = new ResultValidation()
            {
                IsSuccess = true
            };
            decimal data = 0;

            if (value != null && decimal.TryParse(value.ToString(), out data))
            {
                if (data >= this.Min && data <= this.Max)
                {
                    return(ev);
                }
                else
                {
                    ev.ErrorMessage = string.Format(this.ErrorMessage, this.Min, this.Max);
                    ev.IsSuccess    = false;
                }
            }
            else
            {
                ev.ErrorMessage = this.ErrorMessage;
                ev.IsSuccess    = false;
            }
            return(ev);;
        }
コード例 #3
0
        /// <summary>
        /// 验证方法
        /// </summary>
        /// <param name="value">需要验证的值</param>
        /// <returns>返回验证结果</returns>
        public ResultValidation Validation(object value)
        {
            //判断是否设置了异常提示信息,如果没有,设置默认值
            if (string.IsNullOrEmpty(this.ErrorMessage))
            {
                this.ErrorMessage = @"字段值不能为空";
            }

            ResultValidation ev = new ResultValidation()
            {
                IsSuccess = true
            };

            if (value == null || (value != null && string.IsNullOrEmpty(value.ToString())))
            {
                ev.IsSuccess    = false;
                ev.ErrorMessage = this.ErrorMessage;
            }
            return(ev);
        }