C#中各种验证方法(数字,邮件,电话,传真,邮政编码,网络地址)和自动编号的

        #region  验证输入字符串为数字
        /// <summary>
        /// 验证输入字符串为数字
        /// </summary>
        /// <param name="P_str_num">输入字符</param>
        /// <returns>返回一个bool类型的值</returns>
        public bool validateNum(string P_str_num)
        {
            return Regex.IsMatch(P_str_num, "^[0-9]*$");
        }
        #endregion

        #region  验证输入字符串为电话号码
        /// <summary>
        /// 验证输入字符串为电话号码
        /// </summary>
        /// <param name="P_str_phone">输入字符串</param>
        /// <returns>返回一个bool类型的值</returns>
        public bool validatePhone(string P_str_phone)
        {
            return Regex.IsMatch(P_str_phone, @"\d{3,4}-\d{7,8}");
        }
        #endregion

        #region  验证输入字符串为传真号码
        /// <summary>
        /// 验证输入字符串为传真号码
        /// </summary>
        /// <param name="P_str_fax">输入字符串</param>
        /// <returns>返回一个bool类型的值</returns>
        public bool validateFax(string P_str_fax)
        {
            return Regex.IsMatch(P_str_fax, @"86-\d{2,3}-\d{7,8}");
        }
        #endregion

        #region  验证输入字符串为邮政编码
        /// <summary>
        /// 验证输入字符串为邮政编码
        /// </summary>
        /// <param name="P_str_postcode">输入字符串</param>
        /// <returns>返回一个bool类型的值</returns>
        public bool validatePostCode(string P_str_postcode)
        {
            return Regex.IsMatch(P_str_postcode, @"\d{6}");
        }
        #endregion

        #region  验证输入字符串为E-mail地址
        /// <summary>
        /// 验证输入字符串为E-mail地址
        /// </summary>
        /// <param name="P_str_email">输入字符串</param>
        /// <returns>返回一个bool类型的值</returns>
        public bool validateEmail(string P_str_email)
        {
            return Regex.IsMatch(P_str_email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }
        #endregion

        #region  验证输入字符串为网络地址
        /// <summary>
        /// 验证输入字符串为网络地址
        /// </summary>
        /// <param name="P_str_naddress">输入字符串</param>
        /// <returns>返回一个bool类型的值</returns>
        public bool validateNAddress(string P_str_naddress)
        {
            return Regex.IsMatch(P_str_naddress, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
        }
        #endregion

        #region  自动编号
        /// <summary>
        /// 自动编号
        /// </summary>
        /// <param name="P_str_sqlstr">SQL语句</param>
        /// <param name="P_str_table">数据表名</param>
        /// <param name="P_str_tbColumn">数据表字段</param>
        /// <param name="P_str_codeIndex">编号前的字符串</param>
        /// <param name="P_str_codeNum">编号后面的数字</param>
        /// <param name="txt">TextBox控件名</param>
        public void autoNum(string P_str_sqlstr,string P_str_table,string P_str_tbColumn,string P_str_codeIndex,string P_str_codeNum,TextBox txt)
        {
            string P_str_Code = "";
            int P_int_Code = 0;
            DataSet myds = boperate.getds(P_str_sqlstr,P_str_table);
            if (myds.Tables[0].Rows.Count == 0)
            {
                txt.Text = P_str_codeIndex + P_str_codeNum;
            }
            else
            {
                P_str_Code = Convert.ToString(myds.Tables[0].Rows[myds.Tables[0].Rows.Count - 1][P_str_tbColumn]);
                P_int_Code = Convert.ToInt32(P_str_Code.Substring(2, 7)) + 1;
                P_str_Code = P_str_codeIndex + P_int_Code.ToString();
                txt.Text = P_str_Code;
            }
        }
        #endregion

在窗体中可以通过if(!公共类的命名空间.validatePhone(txt.text.trim()))来用

你可能感兴趣的:(C#)