C#-通用数据验证类

来源:互联网 发布:网络短信发送平台 编辑:程序博客网 时间:2024/05/30 23:07

引入通用验证类命名控件

using System.Text.RegularExpressions;
这里写图片描述


验证是否正整数

这里写图片描述

验证是否Email

这里写图片描述


验证身份证

这里写图片描述


using System;using System.Collections.Generic;using System.Linq;using System.Text;//引入命名空间using System.Text.RegularExpressions;namespace StudentManagePro.Common{    /// <summary>    /// 通用验证类    /// </summary>     public class DataValidate    {        /// <summary>        /// 验证正整数        /// </summary>              public static bool IsInteger(string txt)        {            Regex objReg = new Regex(@"^[1-9]\d*$");            return objReg.IsMatch(txt);        }        /// <summary>        /// 验证是否Email        /// </summary>             public static bool IsEmail(string txt)        {            Regex objReg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");            return objReg.IsMatch(txt);        }        /// <summary>        /// 验证身份证        /// </summary>                public static bool IsIdentityCard(string txt)        {            Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");             return objReg.IsMatch(txt);        }    }}

正则表达式文档

原创粉丝点击