using system;using system.text.regularexpressions;namespace zmm.core{ /// <summary> /// 验证帮助类 /// </summary> public class validatehelper { //邮件正则表达式 private static regex _emailregex = new regex(@^[a-z0-9]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$, regexoptions.ignorecase); //手机号正则表达式 private static regex _mobileregex = new regex(^(13|15|17|18)[0-9]{9}$); //固话号正则表达式 private static regex _phoneregex = new regex(@^(\d{3,4}-?)?\d{7,8}$); //ip正则表达式 private static regex _ipregex = 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])$); //日期正则表达式 private static regex _dateregex = new regex(@(\d{4})-(\d{1,2})-(\d{1,2})); //数值(包括整数和小数)正则表达式 private static regex _numericregex = new regex(@^[-]?[0-9]+(\.[0-9]+)?$); //邮政编码正则表达式 private static regex _zipcoderegex = new regex(@^\d{6}$); /// <summary> /// 是否为邮箱名 /// </summary> public static bool isemail(string s) { if (string.isnullorempty(s)) return true; return _emailregex.ismatch(s); } /// <summary> /// 是否为手机号 /// </summary> public static bool ismobile(string s) { if (string.isnullorempty(s)) return true; return _mobileregex.ismatch(s); } /// <summary> /// 是否为固话号 /// </summary> public static bool isphone(string s) { if (string.isnullorempty(s)) return true; return _phoneregex.ismatch(s); } /// <summary> /// 是否为ip /// </summary> public static bool isip(string s) { return _ipregex.ismatch(s); } /// <summary> /// 是否是身份证号 /// </summary> public static bool isidcard(string id) { if (string.isnullorempty(id)) return true; if (id.length == 18) return checkidcard18(id); else if (id.length == 15) return checkidcard15(id); else return false; } /// <summary> /// 是否为18位身份证号 /// </summary> private static bool checkidcard18(string id) { long n = 0; if (long.tryparse(id.remove(17), out n) == false || n < math.pow(10, 16) || long.tryparse(id.replace('x', '0').replace('x', '0'), out n) == false) return false;//数字验证 string address = 11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91; if (address.indexof(id.remove(2)) == -1) return false;//省份验证 string birth = id.substring(6, 8).insert(6, -).insert(4, -); datetime time = new datetime(); if (datetime.tryparse(birth, out time) == false) return false;//生日验证 string[] arrvarifycode = (1,0,x,9,8,7,6,5,4,3,2).split(','); string[] wi = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2).split(','); char[] ai = id.remove(17).tochararray(); int sum = 0; for (int i = 0; i < 17; i++) sum += int.parse(wi[i]) * int.parse(ai[i].tostring()); int y = -1; math.divrem(sum, 11, out y); if (arrvarifycode[y] != id.substring(17, 1).tolower()) return false;//校验码验证 return true;//符合gb11643-1999标准 } /// <summary> /// 是否为15位身份证号 /// </summary> private static bool checkidcard15(string id) { long n = 0; if (long.tryparse(id, out n) == false || n 0) { datetime starttime; datetime endtime; datetime nowtime = datetime.now; datetime nowdate = nowtime.date; foreach (string period in periodlist) { int index = period.indexof(-); starttime = typehelper.stringtodatetime(period.substring(0, index)); endtime = typehelper.stringtodatetime(period.substring(index + 1)); if (starttime < endtime) { if (nowtime > starttime && nowtime < endtime) { lieperiod = period; return true; } } else { if ((nowtime > starttime && nowtime < nowdate.adddays(1)) || (nowtime 0) { foreach (string numberstr in numericstrlist) { if (!isnumeric(numberstr)) return false; } return true; } return false; } /// <summary> /// 是否是数值(包括整数和小数) /// </summary> public static bool isnumericrule(string numericrulestr, string splitchar) { return isnumericarray(stringhelper.splitstring(numericrulestr, splitchar)); } /// <summary> /// 是否是数值(包括整数和小数) /// </summary> public static bool isnumericrule(string numericrulestr) { return isnumericrule(numericrulestr, ,); } }}
以上就是如何验证数据帮助类?验证数据帮助类的方法(代码示例)的详细内容。
