asp.net中常用信息验证总结
2012-03-13 11:32
239 查看
/// <summary> /// 判断字符串是否是字母数字下划线组成 /// </summary> public static bool IsNumWord(string inString) { Regex regex = new Regex("^[0-9a-zA-Z_]{1,}$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是正的浮点数、是否是零 /// </summary> public static bool IsPositiveFloatAndZero(string inString) { Regex regex = new Regex("^\\d+(\\.\\d+)?$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是正的浮点数 /// </summary> private static bool IsPositive(string inString) { Regex regex = new Regex("^[0-9]*[1-9][0-9]*$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是日期格式“年-月-日” /// </summary> public static bool IsDate(string inString) { Regex regex = new Regex("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是正整数 /// </summary> public static bool IsInt(string inString) { Regex regex = new Regex("^[0-9]*[1-9][0-9]*$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是中文 /// </summary> public static bool IsChinese(string inString) { Regex regex = new Regex("[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是电话号码 /// </summary> public static bool IsTel(string inString) { Regex regex = new Regex(@"(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[358]\d{9})$)"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是手机号码 /// </summary> public static bool IsMobilePhone(string inString) { Regex regex = new Regex(@"(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[358]\d{9})$)"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是Email /// </summary> public static bool IsEmail(string inString) { Regex regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是邮政编码 /// </summary> public static bool IsPostalcode(string inString) { Regex regex = new Regex("^\\d{6}$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是身份证码 /// </summary> public static bool IsNumberCard(string inString) { Regex regex = new Regex(@"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是网址 /// </summary> public static bool IsIntnetUrl(string inString) { Regex regex = new Regex("^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"); return regex.IsMatch(inString.Trim()); } /// <summary> /// 判断字符串是否是IP /// </summary> public static bool IsIP(string inString) { Regex regex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); return regex.IsMatch(inString.Trim()); //Regex regex = new Regex(@"\b(((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])(\b|\.)){4}"); //return regex.IsMatch(inString.Trim()); }
相关文章推荐
- ASP.NET中常用功能代码总结(2)——生成验证图片
- ASP.NET中常用功能代码总结(2)——生成验证图片
- asp.net中几种常用的身份验证方法总结
- ASP.NET中常用功能代码总结(2)——生成验证图片
- ASP.NET中常用功能代码总结(2)——生成验证图片
- 【Asp.Net MVC】asp.net mvc Model验证总结及常用正则表达式
- 【转】【Asp.Net MVC】asp.net mvc Model验证总结及常用正则表达式
- ASP.NET中常用功能代码总结(生成验证图片)
- asp.net mvc Model验证总结及常用正则表达式
- ASP.NET中常用功能代码总结(2)——生成验证图片
- ASP.NET中常用功能代码总结(生成验证图片)
- ASP.NET中常用功能代码总结——生成验证图片
- 【转】【Asp.Net MVC】asp.net mvc Model验证总结及常用正则表达式
- asp.net mvc Model验证总结及常用正则表达式
- ASP.NET验证控件学习总结
- ASP.NET验证控件学习总结与正则表达式学习入门
- 重温ASP.NET的配置信息(简单、常用)
- ASP.NET 常用状态(信息)保存方式
- ASP.NET中常用功能代码总结(6)——XML文件操作篇