.net后台验证类
/// <summary> /// 提供经常需要使用的一些验证逻辑。 比如 邮箱是否合法 /// </summary> public class Validator { /// <summary> /// 检查一个字符串是否可以转化为日期,一般用于验证用户输入日期的合法性。 /// </summary> /// <param name="_value">需验证的字符串。</param> /// <returns>是否可以转化为日期的bool值。</returns> public static bool IsStringDate(string _value) { DateTime dTime; try { dTime = DateTime.Parse(_value); } catch (FormatException e) { //日期格式不正确时 Console.WriteLine(e.Message); return false; } return true; } /// <summary> /// 检测是否有中文字符 /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static bool IsHasCHZN(string inputData) { Regex RegCHZN = new Regex("[/u4e00-/u9fa5]"); Match m = RegCHZN.Match(inputData); return m.Success; } /// <summary> /// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。 /// </summary> /// <param name="_value">需验证的字符串。。</param> /// <returns>是否合法的bool值。</returns> public static bool IsNumeric(string _value) { return Validator.QuickValidate("^[1-9]*[0-9]*$", _value); } /// <summary> /// 检查一个字符串是否是纯字母和数字构成的,一般用于查询字符串参数的有效性验证。 /// </summary> /// <param name="_value">需验证的字符串。</param> /// <returns>是否合法的bool值。</returns> public static bool IsLetterOrNumber(string _value) { return Validator.QuickValidate("^[a-zA-Z0-9_]*$", _value); } /// <summary> /// 判断是否是数字,包括小数和整数。 /// </summary> /// <param name="_value">需验证的字符串。</param> /// <returns>是否合法的bool值。</returns> public static bool IsNumber(string _value) { return Validator.QuickValidate("^(0|([1-9]+[0-9]*))(.[0-9]+)?$", _value); } /// <summary> /// 快速验证一个字符串是否符合指定的正则表达式。 /// </summary> /// <param name="_express">正则表达式的内容。</param> /// <param name="_value">需验证的字符串。</param> /// <returns>是否合法的bool值。</returns> public static bool QuickValidate(string _express, string _value) { System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex(_express); if (_value.Length == 0) { return false; } return myRegex.IsMatch(_value); } /// <summary> /// 判断一个字符串是否为邮件 /// </summary> /// <param name="_value"></param> /// <returns></returns> public static bool IsEmail(string _value) { Regex regex = new Regex(@"^/w+([-+.]/w+)*@(/w+([-.]/w+)*/.)+([a-zA-Z]+)+$", RegexOptions.IgnoreCase); return regex.Match(_value).Success; } /// <summary> /// 判断一个字符串是否为ID格式 /// </summary> /// <param name="_value"></param> /// <returns></returns> public static bool IsIDCard(string _value) { Regex regex; string[] strArray; DateTime time; if ((_value.Length != 15) && (_value.Length != 0x12)) { return false; } if (_value.Length == 15) { regex = new Regex(@"^(/d ![]() ![]() ![]() ![]() ![]() if (!regex.Match(_value).Success) { return false; } strArray = regex.Split(_value); try { time = new DateTime(int.Parse("19" + strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4])); return true; } catch { return false; } } regex = new Regex(@"^(/d ![]() ![]() ![]() ![]() ![]() if (!regex.Match(_value).Success) { return false; } strArray = regex.Split(_value); try { time = new DateTime(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4])); return true; } catch { return false; } } /// <summary> /// 判断一个字符串是否为Int /// </summary> /// <param name="_value"></param> /// <returns></returns> public static bool IsInt(string _value) { Regex regex = new Regex(@"^(-){0,1}/d+$"); if (regex.Match(_value).Success) { if ((long.Parse(_value) > 0x7fffffffL) || (long.Parse(_value) < -2147483648L)) { return false; } return true; } return false; } /// <summary> /// 判断一个字符串是否为手机号码 /// </summary> /// <param name="_value"></param> /// <returns></returns> public static bool IsMobileNum(string _value) { Regex regex = new Regex(@"^13/d ![]() return regex.Match(_value).Success; } /// <summary> /// 判断一个字符串是否为电话号码 /// </summary> /// <param name="_value"></param> /// <returns></returns> public static bool IsPhoneNum(string _value) { Regex regex = new Regex(@"^(86)?(-)?(0/d{2,3})?(-)?(/d{7,8})(-)?(/d{3,5})?$", RegexOptions.IgnoreCase); return regex.Match(_value).Success; } /// <summary> /// 判断一个字符串是否为网址 /// </summary> /// <param name="_value"></param> /// <returns></returns> public static bool IsUrl(string _value) { Regex regex = new Regex(@"(http://)?([/w-]+/.)*[/w-]+(/[/w- ./?%&=]*)?", RegexOptions.IgnoreCase); return regex.Match(_value).Success; } /// <summary> /// 判断一个字符串是否为字母加数字 /// Regex("[a-zA-Z0-9]?" /// </summary> /// <param name="_value"></param> /// <returns></returns> public static bool IsWordAndNum(string _value) { Regex regex = new Regex("[a-zA-Z0-9]?"); return regex.Match(_value).Success; } /// <summary> /// 把字符串转成日期 /// </summary> /// <param name="_value">字符串</param> /// <param name="_defaultValue">默认值</param> /// <returns></returns> public static DateTime StrToDate(string _value, DateTime _defaultValue) { if (IsStringDate(_value)) return Convert.ToDateTime(_value); else return _defaultValue; } /// <summary> /// 把字符串转成整型 /// </summary> /// <param name="_value">字符串</param> /// <param name="_defaultValue">默认值</param> /// <returns></returns> public static int StrToInt(string _value, int _defaultValue) { if (IsNumber(_value)) return int.Parse(_value); else return _defaultValue; } } |
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 有关前台验证和后台验证
- 下一篇: 注册登录——前端、后台验证(正则表达式)