public sealed class ValidateForm ...{ //判断字符是否为数字#region //判断字符是否为数字 /**//// <summary> /// 判断字符是否为数字 /// </summary> /// <param name="_strNum">传入的字符串</param> /// <returns>boolean</returns> public static bool isNumeric(string _strNum) ...{ if (_strNum == null || _strNum == string.Empty) return false; for (int i = 0; i < _strNum.Length; i++) ...{ if (!Char.IsNumber(_strNum, i)) return false; } return true; } #endregion /**//// <summary> /// 验证Email地址 /// </summary> /// <param name="strIn"></param> /// <returns></returns> public static bool IsValidEmail(string strIn) ...{ // Return true if strIn is in valid e-mail format. return Regex.IsMatch(strIn, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$"); } /**//// <summary> /// dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。 /// </summary> /// <param name="input"></param> /// <returns></returns> public static string MDYToDMY(String input) ...{ return Regex.Replace(input, "(?d{1,2})/(?d{1,2})/(?d{2,4})", "${day}-${month}-${year}"); } //验证是否为小数 public static bool IsValidDecimal(string strIn) ...{ return Regex.IsMatch(strIn, @"[0].d{1,2}|[1]"); } //验证是否为电话号码 public static bool IsValidTel(string strIn) ...{ return Regex.IsMatch(strIn, @"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?"); } //验证年月日 public static bool IsValidDate(string strIn) ...{ return Regex.IsMatch(strIn, @"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$"); } //验证后缀名 public static bool IsValidPostfix(string strIn) ...{ return Regex.IsMatch(strIn, @".(?i:gif|jpg)$"); } //验证字符是否再4至12之间 public static bool IsValidByte(string strIn) ...{ return Regex.IsMatch(strIn, @"^[a-z]{4,12}$"); } //验证IP public static bool IsValidIp(string strIn) ...{ return Regex.IsMatch(strIn, @"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$"); } public static bool IsURL(string URL) ...{ return Regex.IsMatch(URL ,"((^http)|(^https)|(^ftp))://(w)+.(w)+"); } 判断号码是联通,移动,电信中的哪个,在使用本方法前,请先验证号码的合法性#region 判断号码是联通,移动,电信中的哪个,在使用本方法前,请先验证号码的合法性 /**//// <summary> /// 判断号码是联通,移动,电信中的哪个,在使用本方法前,请先验证号码的合法性 /// 规则:前三位为130-133 联通 ;前三位为135-139或前四位为1340-1348 移动; 其它的应该为电信 /// </summary> /// <param name="mobile">要判断的号码</param> /// <returns>int 返回相应类型:1代表联通;2代表移动;3代表电信</returns> public static int GetMobileType(string mobile) ...{ string[] chinaUnicom = new string[] ...{ "130", "131", "132", "133" }; string[] chinaMobile1 = new string[] ...{ "135", "136", "137", "138", "139" }; string[] chinaMobile2 = new string[] ...{ "1340", "1341", "1342", "1343", "1344", "1345", "1346", "1347", "1348" }; bool bolChinaUnicom = (Array.IndexOf(chinaUnicom, mobile.Substring(0, 3)) >= 0); bool bolChinaMobile1 = (Array.IndexOf(chinaMobile1, mobile.Substring(0, 3)) >= 0); bool bolChinaMobile2 = (Array.IndexOf(chinaMobile2, mobile.Substring(0, 4)) >= 0); if (bolChinaUnicom) ...{ return 1;//联通 } if (bolChinaMobile1 || bolChinaMobile2) ...{ return 2; //移动 } return 3; //其他为电信 } #endregion 验证输入的18位身份证号码是否正确#region 验证输入的18位身份证号码是否正确 /**//// <summary> /// 18位身份证格式验证算法 /// 公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码. /// 地址码表示编码对象常住户口所在县( 市、旗、区 )的行政区划代码. /// 生日期码表示编码对象出生的年、月、日,其中年份用四位数字表示,年、月、日之间不用分隔符. /// 顺序码表示同一地址码所标识的区域范围内,对同年、月、日出生的人员编定的顺序号.顺序码的奇数分给男性,偶数分给女性. /// 校验码是根据前面十七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出来的检验码. ///调用测试: /// Response.Write( CheckCidInfo( "340524198002300019" ) ); ///Response.Write( CheckCidInfo( "34052419800101001x" ) ); /// </summary> /// <param name="cid">18位身份证号码</param> /// <returns></returns> public static bool CheckCidInfo(string cid) ...{ string[] aCity = new string[] ...{ null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陕西", "甘肃", "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" }; double iSum = 0; System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^d{17}(d|x)$"); System.Text.RegularExpressions.Match mc = rg.Match(cid); if (!mc.Success) ...{ return false; //""; } cid = cid.ToLower(); cid = cid.Replace("x", "a"); if (aCity[int.Parse(cid.Substring(0, 2))] == null) ...{ return false; // "非法地区"; } try ...{ DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2)); } catch ...{ return false; // "非法生日"; } for (int i = 17; i >= 0; i--) ...{ iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber); } if (iSum % 11 != 1) ...{ return false; // ("非法证号"); } return true; //(aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? "男" : "女")); } #endregion }