Пример #1
0
 private void InitValidate(ValidatorItemInfo itemInfo)
 {
     itemInfo.Control.Validating  += new CancelEventHandler(Control_Validating);
     itemInfo.Control.Validated   += new EventHandler(Control_Validated);
     itemInfo.Control.LostFocus   += new EventHandler(Control_LostFocus);
     itemInfo.Control.TextChanged += new EventHandler(Control_TextChanged);
 }
Пример #2
0
        private ValidatorItemInfo EnsureControlItem(Control control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            ValidatorItemInfo item = null;

            if (!this.items.ContainsKey(control))
            {
                item = new ValidatorItemInfo(this, control);
                itemsBackColor.Add(control, control.BackColor);
                InitValidate(item);
                this.items.Add(control, item);
            }
            item = this.items[control];
            return(item);
        }
Пример #3
0
 private bool ValidateLength(ValidatorItemInfo item, string value)
 {
     if (item.MaxLength < 1 && item.MinLength < 1 || item.MaxLength < item.MinLength)
     {
         return(true);
     }
     if (item.MaxLength == item.MinLength)
     {
         return(value.Length == item.MinLength);
     }
     if (item.MaxLength > 0 && item.MinLength < 1)
     {
         return(value.Length <= item.MaxLength);
     }
     if (item.MinLength > 0 && item.MaxLength < 1)
     {
         return(value.Length >= item.MaxLength);
     }
     return(value.Length <= item.MaxLength && value.Length >= item.MinLength);
 }
Пример #4
0
        void Control_Validating(object sender, CancelEventArgs e)
        {
            Control           control = sender as Control;
            ValidatorItemInfo item    = this.items[control];

            if (item.DataType == DataType.未设)
            {
                return;
            }
            //如果不允许为空
            if (!item.AllowNullOrEmpty)
            {
                if (string.IsNullOrWhiteSpace(control.Text))
                {
                    SetErrorBackColor(control);
                    ShowError(control, string.Format("[{0}]不可为空,请您输入[{0}]!", item.DataName), string.Format("验证[{0}]", item.DataName));
                    e.Cancel = true;
                    return;
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(control.Text))
                {
                    return;
                }
            }
            //正则表达式
            { Regex regex = item.GetRegex();
              if (regex != null)
              {
                  bool match = regex.IsMatch(control.Text);
                  if (!match)
                  {
                      ShowError(control, string.Format("[{0}]格式不对,请您输入正确的[{0}]!\r\n{1}", item.DataName, item.DataDescription), string.Format("验证[{0}]格式", item.DataName));
                  }
                  e.Cancel = !match;
                  return;
              }
              if (regex == null)
              {
                  bool validated = ValidateLength(item, control.Text);
                  if (!validated)
                  {
                      ShowError(control, string.Format("您输入的[{0}]长度不对,请您输入正确长度的[{0}]!\r\n{1}", item.DataName, item.DataDescription), string.Format("验证[{0}]格式", item.DataName));
                      e.Cancel = true;
                      return;
                  }
                  //整数,
                  //正整数,
                  //负整数,
                  //非正整数,
                  //非负整数,
                  //浮点数,
                  //正浮点数,
                  //负浮点数,
                  //非正浮点数,
                  //非负浮点数,
                  //中国华人民共和国身份证,
                  //中国华人民共和国邮编,
                  //汉字,
                  //大写字母,
                  //小写字母,
                  //字母,
                  //数字,
                  //字母数字,
                  //大写字母数字,
                  //小写字母数字,
                  //日期,
                  //日期时间,
                  //时间,
                  //字符串,
                  if (item.DataType == DataType.整数 ||
                      item.DataType == DataType.正整数 ||
                      item.DataType == DataType.负整数 ||
                      item.DataType == DataType.非正整数 ||
                      item.DataType == DataType.非负整数 ||
                      item.DataType == DataType.中国华人民共和国身份证 ||
                      item.DataType == DataType.中国华人民共和国邮编 ||
                      item.DataType == DataType.汉字 ||
                      item.DataType == DataType.大写字母 ||
                      item.DataType == DataType.小写字母 ||
                      item.DataType == DataType.字母 ||
                      item.DataType == DataType.数字 ||
                      item.DataType == DataType.字母数字 ||
                      item.DataType == DataType.大写字母数字 ||
                      item.DataType == DataType.日期 ||
                      item.DataType == DataType.日期时间 ||
                      item.DataType == DataType.时间
                      )
                  {
                      //数据格式
                      validated = false;
                      validated = DataRegex.Instance[item.DataType].IsMatch(control.Text);
                      if (!validated)
                      {
                          ShowError(control, string.Format("[{0}]格式不对,请您输入正确的[{0}]!\r\n{1}", item.DataName, item.DataDescription), string.Format("验证[{0}]格式", item.DataName));
                          e.Cancel = true;
                          return;
                      }
                  }
              }
            }
            balloon.Hide();
        }