PageValidate 类
转载:http://www.cnblogs.com/sufei/archive/2010/01/14/1648028.html
using System.Text.RegularExpressions; /// <summary> /// 开发人:苏飞 /// 开发时间:2009-09-11 /// 主要功能:判断页面各个控件的输入 /// </summary> namespace BaseFunction { public class PageValidate { public PageValidate() { // // TODO: 在此处添加构造函数逻辑 // } private static Regex RegNumber = new Regex("^[0-9]+$"); private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$"); private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$"); private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$ private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); public static Regex RegMobile = new Regex("^1(3[4-9]|5[012789]|8[7-8])\\d{8}$"); //验证联通手机号码 public static Regex RegUnicom = new Regex("^1(3[012]|5[56]|8[5-6])\\d{8}$"); //验证联通固话 #region 日期检查 /// <summary> /// Format 的摘要说明。 /// </summary> public string IsDateTime(string Record) { if (Convert.ToDateTime(Record).ToString("yyyy-MM-dd") == DateTime.MaxValue.ToString("yyyy-MM-dd") || Convert.ToDateTime(Record).ToString("yyyy-MM-dd") == DateTime.MinValue.ToString("yyyy-MM-dd") || Record == "" || Convert.ToDateTime(Record).ToString("yyyy-MM-dd") == "1900-01-01") return "—"; else return Convert.ToDateTime(Record).ToString("yyyy-M-dd"); } #endregion #region 数字字符串检查 /// <summary> /// 是否数字字符串 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsNumber(string inputData) { Match m = RegNumber.Match(inputData); return m.Success; } /// <summary> /// 是否数字字符串 可带正负号 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsNumberSign(string inputData) { Match m = RegNumberSign.Match(inputData); return m.Success; } /// <summary> /// 是否是浮点数 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsDecimal(string inputData) { Match m = RegDecimal.Match(inputData); return m.Success; } /// <summary> /// 是否是浮点数 可带正负号 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsDecimalSign(string inputData) { Match m = RegDecimalSign.Match(inputData); return m.Success; } #endregion #region 中文检测 /// <summary> /// 检测是否有中文字符 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsHasCHZN(string inputData) { Match m = RegCHZN.Match(inputData); return m.Success; } #endregion #region 邮件地址 /// <summary> /// 是否是浮点数 可带正负号 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns></returns> public static bool IsEmail(string inputData) { Match m = RegEmail.Match(inputData); return m.Success; } /// <summary> /// 验证是否为移动号码 /// </summary> /// <param name="number">手机号</param> /// <returns>Boolean</returns> public static Boolean isMobile(string number) { Match m = RegMobile.Match(number); return m.Success; } /// <summary> /// 验证是否为联通号码 /// </summary> /// <param name="number">手机号</param> /// <returns></returns> public static Boolean IsUnicom(string number) { Match m = RegUnicom.Match(number); return m.Success; } #endregion #region 其他 /// <summary> /// 检查字符串最大长度,返回指定长度的串 /// </summary> /// <param name="sqlInput">输入字符串</param> /// <param name="maxLength">最大长度</param> /// <returns></returns> public static string SqlText(string sqlInput, int maxLength) { if (sqlInput != null && sqlInput != string.Empty) { sqlInput = sqlInput.Trim(); if (sqlInput.Length > maxLength)//按最大长度截取字符串 sqlInput = sqlInput.Substring(0, maxLength); } return sqlInput; } #endregion } }
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET