通用验证类

csharp

浏览数:233

2019-1-7

AD:资源代下载服务

片段 1片段 2


验证数字、邮箱、身份证号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//引入命名空间
using System.Text.RegularExpressions;

namespace _9_30.Common
{
    /// <summary>
    /// 基于正则表达式的验证
    /// </summary>
    public class DateValidate
    {
        /// <summary>
        /// 验证正整数
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static bool IsInteger(string txt)
        {
            Regex objRegex = new Regex(@"^[1-9]\d*$");
            return objRegex.IsMatch(txt);
        }

        /// <summary>
        /// 验证是否Email
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static bool IsEmail(string txt)
        {
            Regex objReg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return objReg.IsMatch(txt);
        }
        
        public static bool IsIdentityCard(string txt)
        {
            Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
            return objReg.IsMatch(txt);
        }
    }
}


gistfile1.txt

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//引入命名空间
using System.Text.RegularExpressions;

namespace _9_30.Common
{
    /// <summary>
    /// 基于正则表达式的验证
    /// </summary>
    public class DateValidate
    {
        /// <summary>
        /// 验证正整数
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static bool IsInteger(string txt)
        {
            Regex objRegex = new Regex(@"^[1-9]\d*$");
            return objRegex.IsMatch(txt);
        }

        /// <summary>
        /// 验证是否Email
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static bool IsEmail(string txt)
        {
            Regex objReg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return objReg.IsMatch(txt);
        }
        
        public static bool IsIdentityCard(string txt)
        {
            Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
            return objReg.IsMatch(txt);
        }
    }
}