欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 常用验证

程序员文章站 2022-07-26 19:38:46
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.UI.HtmlControls; using System... ......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace Common
{
    public class Validate
    {
        private static readonly Regex RegPhone = new Regex("(^(\\d{11})$|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)");  //电话号码和手机验证
        private static Regex RegEmail = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@([\\w-]+\\.)+\\w{2,3})\\s*$"); //new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
        private static Regex RegNum = new Regex("^[0-9]+$");

        //必须是数字正则表达式(小数或整数)
        private static Regex regex = new Regex(@"/^[0-9]+\.?[0-9]{0,3}$/");
        /// <summary>
        /// 身份证正值表达式
        /// </summary>
        private static readonly Regex RegCardId = new Regex("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)");
        
        #region 确定用户输入是否合法
        /// <summary>
        /// 确定用户输入是否合法
        /// </summary>
        /// <param name="text">用户输入字符串</param>
        /// <param name="maxLength">最大字符串长度</param>
        /// <returns></returns>
        public static string InputText(string text, int maxLength)
        {
            if (string.IsNullOrEmpty(text))
                return string.Empty;
            text = text.Trim();
            if (maxLength != 0)
                if (text.Length > maxLength)
                    text = text.Substring(0, maxLength);
            text = Regex.Replace(text, "[\\s]{2,}", " ");
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ");
            //text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //屏蔽标签
            text = text.Replace("'", "''");
            return text;
        }
        #endregion

        #region 验证电话号码
        // 电话号码和手机号码检查
        /// <summary>
        /// 电话号码和手机号码检查
        /// </summary>
        /// <param name="inputData">电话号码或手机号码</param>
        /// <returns>匹配结果</returns>
        public static bool IsPhone(string inputData)
        {
            Match m = RegPhone.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 验证参数是否为中文字符
        /// <summary>
        /// 验证参数是否为中文字符
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns></returns>
        public static bool IsChinese(string input)
        {
            Regex regex = new Regex(@"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase);
            return regex.IsMatch(input);
        }
        #endregion

        #region 邮件地址

        /// <summary>
        /// 邮件地址验证
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns>验证结果</returns>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 是否为数字
        /// <summary>
        /// 是否为数字
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns>是否为数字</returns>
        public static bool IsNum(string inputData)
        {
            if(string.IsNullOrEmpty(inputData))
            {
                return false;
            }
            Match m = RegNum.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 判断是否是整数或小数
        /// </summary>
        /// <param name="str">输入的字符串</param>
        /// <returns>是否为数字</returns>
        public static bool IsNumAll(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return false;
            }
            Match m = regex.Match(str);
            return m.Success;
        }

        #endregion

        #region 是否为身份证
        /// <summary>
        /// 是否为身份证
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns>是否为身份证</returns>
        public static bool IsCardId(string inputData)
        {
            Match m = RegCardId.Match(inputData);
            return m.Success;
        }
        #endregion


        /// <summary>
        /// 判断字符串是否是纯数字
        /// </summary>
        /// <param name="message">源字符串</param>
        /// <returns></returns>
        public static bool IsNumberic(string message)//, out int result
        {
            System.Text.RegularExpressions.Regex rex =
            new System.Text.RegularExpressions.Regex(@"^\d+$");
            var result = -1;
            if (rex.IsMatch(message))
            {
                result = int.Parse(message);
                return true;
            }
            else
                return false;
        }
    }
}