关于验证

来源:互联网 发布:淘宝扣分清零时间 编辑:程序博客网 时间:2024/05/17 20:30
    /*----------------------     *     * 使用方法     *    1) 建立新窗口    2) 使用VadTextBox    3) 设置属性:            vadTextBox1.VadFormat = VadString.DateTime("-", " ", ":");            vadTextBox1.MatchStart = true;            vadTextBox1.MatchStop = true;            vadTextBox1.ErrorInfo = " Try Again";    4) 添加普通按钮    5) click 事件            foreach (Control control in this.Controls)            {                // Set focus on control                control.Focus();                // Validate causes the control's Validating event to be fired,                // if CausesValidation is True                if (!Validate())                {                    DialogResult = DialogResult.None;                    return;                }            }    -----------------------*/} using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Windows.Forms;namespace Validator{    #region [ VadString ]    public class VadString    {        #region [ 构造函数 ]        public VadString()        {        }        #endregion        #region [ 起、止 ]        static public string Start        {            get            {                return "^";            }        }        static public string Stop        {            get            {                return "$";            }        }        #endregion        #region [ 数值 ]        static public string Digit        {            get            {                return "//d";            }        }        static public string Digits        {            get            {                return "//d*";            }        }        static public string DigitN (string N)        {            return "//d{"+N+"}";                   }        static public string DigitN(string N1,string N2)        {            return "//d{" + N1+","+N2 + "}";        }        static public string IntegerPos        {            get            {                return "//d+";            }        }        static public string IntegerNeg        {            get            {                return "-//d+";            }        }        static public string IntegerAll        {            get            {                return "-?//d+";            }        }        static public string FloatPos        {            get            {                return "//d+(//.//d+)?";            }        }        static public string FloatNeg        {            get            {                return "((-//d+(//.//d+)?)|(0+(//.0+)?))";            }        }        static public string FloatAll        {            get            {                return "-?//d+(//.//d+)?";            }        }        #endregion        #region [ 字符 ]        static public string AlphabetDown        {            get            {                return "[a-z]+";            }        }        static public string AlphabetDownN(string N)        {            return "[a-z]{" + N + "}";        }        static public string AlphabetDownN(string N1,string N2)        {            return "[a-z]{" + N1+","+N2 + "}";        }        static public string AlphabetUp        {            get            {                return "[A-Z]+";            }        }        static public string AlphabetUpN(string N)        {            return "[A-Z]{" + N + "}";        }        static public string AlphabetUpN(string N1,string N2)        {            return "[A-Z]{" + N1+","+N2 + "}";        }        static public string AlphabetAll        {            get            {                return "[A-Za-z]+";            }        }        static public string AlphabetAllN(string N)        {            return "[A-Za-z]{" + N + "}";        }        static public string AlphabetAllN(string N1,string N2)        {            return "[A-Za-z]{" + N1+","+N2+ "}";        }        static public string AlphabetAny        {            get            {                return "//w+";            }        }        static public string AlphabetAnyN(string N)        {            return "//w{" + N + "}";        }        static public string AlphabetAnyN(string N1,string N2)        {            return "//w{" + N1+","+N2 + "}";        }        #endregion        #region [ 日期、时间 ]        static public string Year        {            get            {                return "//d{4}";            }        }        static public string Month        {            get            {                return "((0?([1-9]{1}))|(1[1|2]))";            }        }        static public string Date        {            get            {                return "(([0-2]([1-9]{1}))|(3[0|1]))";            }        }                static public string Hour        {            get            {                return "(([0-1]([1-9]{1}))|(2[0-4]))";            }        }        static public string Min        {            get            {                return "[0-5]([1-9]{1})";            }        }        static public string Second        {            get            {                return "[0-5]([1-9]{1})";            }        }        static public string ShortDate(string spliter)        {            return Year + spliter + Month + spliter + Date;        }        static public string LongDate        {            get            {                return "((((1[6-9]|[2-9]//d)//d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]//d|3[01]))|(((1[6-9]|[2-9]//d)//d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]//d|30))|(((1[6-9]|[2-9]//d)//d{2})-0?2-(0?[1-9]|1//d|2[0-8]))|(((1[6-9]|[2-9]//d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))";            }        }        static public string ShortTime(string spliter)        {            return Hour + spliter + Min + spliter + Second;        }        static public string DateTime(string DateSpliter, string spliter, string TimeSpliter)        {            return ShortDate(DateSpliter) + spliter + ShortTime(TimeSpliter);        }        #endregion    }    #endregion    #region [ VadTextBox ]    [ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]    public class VadTextBox:TextBox    {        string format;        bool iStart;        bool iStop;        string ErrorInformation;        ErrorProvider errorProvider1;                #region [ 构造函数 ]        public VadTextBox()        {            format = string.Empty;            iStart = false;            iStop = false;            ErrorInformation = "Invalid Input!";            errorProvider1 = new ErrorProvider();            this.Validating+=new System.ComponentModel.CancelEventHandler(VadTextBox_Validating);        }        #endregion        #region [ 验证属性设置 ]        public string VadFormat        {            get            {                return format;            }            set            {                format = value;            }        }        public bool MatchStart        {            get            {                return iStart;            }            set            {                iStart = value;            }        }        public bool MatchStop        {            get            {                return iStop;            }            set            {                iStop = value;            }        }        public string ErrorInfo        {            get            {                return ErrorInformation;            }            set            {                ErrorInformation = value;            }        }        #endregion        #region [ 验证事件 ]        private void VadTextBox_Validating(object sender, CancelEventArgs e)        {            if (format == string.Empty) return;            string RegexFormat = format; //基本验证格式            if (iStart)                RegexFormat = "^" + RegexFormat;//匹配开头            if (iStop)                RegexFormat += "$";//匹配结尾            //验证过程            if (!Regex.IsMatch(this.Text, RegexFormat))            {                errorProvider1.SetError(this, ErrorInformation);            }            else            {                errorProvider1.SetError(this, null);            }        }        #endregion    }    #endregion  } 

0 0
原创粉丝点击