首页 > 代码库 > .net使用正则表达式校验、匹配字符工具类

.net使用正则表达式校验、匹配字符工具类

开发程序离不开数据的校验,这里整理了一些数据的校验、匹配的方法:

  1     /// <summary>  2     /// 字符(串)验证、匹配工具类  3     /// </summary>  4     public class ValidatorTools  5     {  6         #region 匹配方法  7         /// <summary>    8         /// 验证字符串是否匹配正则表达式描述的规则    9         /// </summary>   10         /// <param name="inputStr">待验证的字符串</param>   11         /// <param name="patternStr">正则表达式字符串</param>   12         /// <returns>是否匹配</returns>   13         public static bool IsMatch(string inputStr, string patternStr) 14         { 15             return IsMatch(inputStr, patternStr, false, false); 16         } 17  18         /// <summary>   19         /// 验证字符串是否匹配正则表达式描述的规则   20         /// </summary>   21         /// <param name="inputStr">待验证的字符串</param>   22         /// <param name="patternStr">正则表达式字符串</param>   23         /// <param name="ifIgnoreCase">匹配时是否不区分大小写</param>   24         /// <returns>是否匹配</returns>   25         public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase) 26         { 27             return IsMatch(inputStr, patternStr, ifIgnoreCase, false); 28         } 29  30         /// <summary>   31         /// 验证字符串是否匹配正则表达式描述的规则   32         /// </summary>   33         /// <param name="inputStr">待验证的字符串</param>   34         /// <param name="ifValidateWhiteSpace">是否验证空白字符串</param> 35         /// <param name="patternStr">正则表达式字符串</param>   36         /// <returns>是否匹配</returns>   37         public static bool IsMatch(string inputStr, bool ifValidateWhiteSpace, string patternStr) 38         { 39             return IsMatch(inputStr, patternStr, false, ifValidateWhiteSpace); 40         } 41  42         /// <summary>   43         /// 验证字符串是否匹配正则表达式描述的规则   44         /// </summary>   45         /// <param name="inputStr">待验证的字符串</param>   46         /// <param name="patternStr">正则表达式字符串</param>   47         /// <param name="ifIgnoreCase">匹配时是否不区分大小写</param>   48         /// <param name="ifValidateWhiteSpace">是否验证空白字符串</param>   49         /// <returns>是否匹配</returns>   50         public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase, bool ifValidateWhiteSpace) 51         { 52             if (!ifValidateWhiteSpace && string.IsNullOrWhiteSpace(inputStr))//.NET 4.0 新增IsNullOrWhiteSpace 方法,便于对用户做处理 53                 return false;//如果不要求验证空白字符串而此时传入的待验证字符串为空白字符串,则不匹配   54             Regex regex = null; 55             if (ifIgnoreCase) 56                 regex = new Regex(patternStr, RegexOptions.IgnoreCase);//指定不区分大小写的匹配   57             else 58                 regex = new Regex(patternStr); 59             return regex.IsMatch(inputStr); 60         } 61         #endregion 62  63         #region 验证方法 64         /// <summary>   65         /// 验证数字(double类型)   66         /// [可以包含负号和小数点]   67         /// </summary>   68         /// <param name="input">待验证的字符串</param>   69         /// <returns>是否匹配</returns>   70         public static bool IsNumber(string input) 71         { 72             //string pattern = @"^-?\d+$|^(-?\d+)(\.\d+)?$";   73             //return IsMatch(input, pattern);   74             double d = 0; 75             if (double.TryParse(input, out d)) 76                 return true; 77             else 78                 return false; 79         } 80  81         /// <summary>   82         /// 验证整数   83         /// </summary>   84         /// <param name="input">待验证的字符串</param>   85         /// <returns>是否匹配</returns>   86         public static bool IsInteger(string input) 87         { 88             //string pattern = @"^-?\d+$";   89             //return IsMatch(input, pattern);   90             int i = 0; 91             if (int.TryParse(input, out i)) 92                 return true; 93             else 94                 return false; 95         } 96  97         /// <summary>   98         /// 验证非负整数   99         /// </summary>  100         /// <param name="input">待验证的字符串</param>  101         /// <returns>是否匹配</returns>  102         public static bool IsIntegerNotNagtive(string input)103         {104             //string pattern = @"^\d+$";  105             //return IsMatch(input, pattern);  106             int i = -1;107             if (int.TryParse(input, out i) && i >= 0)108                 return true;109             else110                 return false;111         }112 113         /// <summary>  114         /// 验证正整数  115         /// </summary>  116         /// <param name="input">待验证的字符串</param>  117         /// <returns>是否匹配</returns>  118         public static bool IsIntegerPositive(string input)119         {120             //string pattern = @"^[0-9]*[1-9][0-9]*$";  121             //return IsMatch(input, pattern);  122             int i = 0;123             if (int.TryParse(input, out i) && i >= 1)124                 return true;125             else126                 return false;127         }128 129         /// <summary>  130         /// 验证小数  131         /// </summary>  132         /// <param name="input">待验证的字符串</param>  133         /// <returns>是否匹配</returns>  134         public static bool IsDecimal(string input)135         {136             string pattern = @"^([-+]?[1-9]\d*\.\d+|-?0\.\d*[1-9]\d*)$";137             return IsMatch(input, pattern);138         }139 140         /// <summary>  141         /// 验证只包含英文字母  142         /// </summary>  143         /// <param name="input">待验证的字符串</param>  144         /// <returns>是否匹配</returns>  145         public static bool IsEnglishCharacter(string input)146         {147             string pattern = @"^[A-Za-z]+$";148             return IsMatch(input, pattern);149         }150 151         /// <summary>  152         /// 验证只包含数字和英文字母  153         /// </summary>  154         /// <param name="input">待验证的字符串</param>  155         /// <returns>是否匹配</returns>  156         public static bool IsIntegerAndEnglishCharacter(string input)157         {158             string pattern = @"^[0-9A-Za-z]+$";159             return IsMatch(input, pattern);160         }161 162         /// <summary>  163         /// 验证只包含汉字  164         /// </summary>  165         /// <param name="input">待验证的字符串</param>  166         /// <returns>是否匹配</returns>  167         public static bool IsChineseCharacter(string input)168         {169             string pattern = @"^[\u4e00-\u9fa5]+$";170             return IsMatch(input, pattern);171         }172 173         /// <summary>  174         /// 验证数字长度范围(数字前端的0计长度)  175         /// [若要验证固定长度,可传入相同的两个长度数值]  176         /// </summary>  177         /// <param name="input">待验证的字符串</param>  178         /// <param name="lengthBegin">长度范围起始值(含)</param>  179         /// <param name="lengthEnd">长度范围结束值(含)</param>  180         /// <returns>是否匹配</returns>  181         public static bool IsIntegerLength(string input, int lengthBegin, int lengthEnd)182         {183             //string pattern = @"^\d{" + lengthBegin + "," + lengthEnd + "}$";  184             //return IsMatch(input, pattern);  185             if (input.Length >= lengthBegin && input.Length <= lengthEnd)186             {187                 int i;188                 if (int.TryParse(input, out i))189                     return true;190                 else191                     return false;192             }193             else194                 return false;195         }196 197         /// <summary>  198         /// 验证字符串包含内容  199         /// </summary>  200         /// <param name="input">待验证的字符串</param>  201         /// <param name="withEnglishCharacter">是否包含英文字母</param>  202         /// <param name="withNumber">是否包含数字</param>  203         /// <param name="withChineseCharacter">是否包含汉字</param>  204         /// <returns>是否匹配</returns>  205         public static bool IsStringInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter)206         {207             if (!withEnglishCharacter && !withNumber && !withChineseCharacter)208                 return false;//如果英文字母、数字和汉字都没有,则返回false  209             StringBuilder patternString = new StringBuilder();210             patternString.Append("^[");211             if (withEnglishCharacter)212                 patternString.Append("a-zA-Z");213             if (withNumber)214                 patternString.Append("0-9");215             if (withChineseCharacter)216                 patternString.Append(@"\u4E00-\u9FA5");217             patternString.Append("]+$");218             return IsMatch(input, patternString.ToString());219         }220 221         /// <summary>  222         /// 验证字符串长度范围  223         /// [若要验证固定长度,可传入相同的两个长度数值]  224         /// </summary>  225         /// <param name="input">待验证的字符串</param>  226         /// <param name="lengthBegin">长度范围起始值(含)</param>  227         /// <param name="lengthEnd">长度范围结束值(含)</param>  228         /// <returns>是否匹配</returns>  229         public static bool IsStringLength(string input, int lengthBegin, int lengthEnd)230         {231             //string pattern = @"^.{" + lengthBegin + "," + lengthEnd + "}$";  232             //return IsMatch(input, pattern);  233             if (input.Length >= lengthBegin && input.Length <= lengthEnd)234                 return true;235             else236                 return false;237         }238 239         /// <summary>  240         /// 验证字符串长度范围(字符串内只包含数字和/或英文字母)  241         /// [若要验证固定长度,可传入相同的两个长度数值]  242         /// </summary>  243         /// <param name="input">待验证的字符串</param>  244         /// <param name="lengthBegin">长度范围起始值(含)</param>  245         /// <param name="lengthEnd">长度范围结束值(含)</param>  246         /// <returns>是否匹配</returns>  247         public static bool IsStringLengthOnlyNumberAndEnglishCharacter(string input, int lengthBegin, int lengthEnd)248         {249             string pattern = @"^[0-9a-zA-z]{" + lengthBegin + "," + lengthEnd + "}$";250             return IsMatch(input, pattern);251         }252 253         /// <summary>  254         /// 验证字符串长度范围  255         /// [若要验证固定长度,可传入相同的两个长度数值]  256         /// </summary>  257         /// <param name="input">待验证的字符串</param>  258         /// <param name="withEnglishCharacter">是否包含英文字母</param>  259         /// <param name="withNumber">是否包含数字</param>  260         /// <param name="withChineseCharacter">是否包含汉字</param>  261         /// <param name="lengthBegin">长度范围起始值(含)</param>  262         /// <param name="lengthEnd">长度范围结束值(含)</param>  263         /// <returns>是否匹配</returns>  264         public static bool IsStringLengthByInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter, int lengthBegin, int lengthEnd)265         {266             if (!withEnglishCharacter && !withNumber && !withChineseCharacter)267                 return false;//如果英文字母、数字和汉字都没有,则返回false  268             StringBuilder patternString = new StringBuilder();269             patternString.Append("^[");270             if (withEnglishCharacter)271                 patternString.Append("a-zA-Z");272             if (withNumber)273                 patternString.Append("0-9");274             if (withChineseCharacter)275                 patternString.Append(@"\u4E00-\u9FA5");276             patternString.Append("]{" + lengthBegin + "," + lengthEnd + "}$");277             return IsMatch(input, patternString.ToString());278         }279 280         /// <summary>  281         /// 验证字符串字节数长度范围  282         /// [若要验证固定长度,可传入相同的两个长度数值;每个汉字为两个字节长度]  283         /// </summary>  284         /// <param name="input">待验证的字符串</param>  285         /// <param name="lengthBegin">长度范围起始值(含)</param>  286         /// <param name="lengthEnd">长度范围结束值(含)</param>  287         /// <returns></returns>  288         public static bool IsStringByteLength(string input, int lengthBegin, int lengthEnd)289         {290             //int byteLength = Regex.Replace(input, @"[^\x00-\xff]", "ok").Length;  291             //if (byteLength >= lengthBegin && byteLength <= lengthEnd)  292             //{  293             //    return true;  294             //}  295             //return false;  296             int byteLength = Encoding.Default.GetByteCount(input);297             if (byteLength >= lengthBegin && byteLength <= lengthEnd)298                 return true;299             else300                 return false;301         }302 303         /// <summary>  304         /// 验证日期  305         /// </summary>  306         /// <param name="input">待验证的字符串</param>  307         /// <returns>是否匹配</returns>  308         public static bool IsDateTime(string input)309         {310             DateTime dt;311             if (DateTime.TryParse(input, out dt))312                 return true;313             else314                 return false;315         }316 317         /// <summary>  318         /// 验证固定电话号码  319         /// [3位或4位区号;区号可以用小括号括起来;区号可以省略;区号与本地号间可以用减号或空格隔开;可以有3位数的分机号,分机号前要加减号]  320         /// </summary>  321         /// <param name="input">待验证的字符串</param>  322         /// <returns>是否匹配</returns>  323         public static bool IsTelePhoneNumber(string input)324         {325             string pattern = @"^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$";326             return IsMatch(input, pattern);327         }328 329         /// <summary>  330         /// 验证手机号码  331         /// [可匹配"(+86)013325656352",括号可以省略,+号可以省略,(+86)可以省略,11位手机号前的0可以省略;11位手机号第二位数可以是3、4、5、8中的任意一个]  332         /// </summary>  333         /// <param name="input">待验证的字符串</param>  334         /// <returns>是否匹配</returns>  335         public static bool IsMobilePhoneNumber(string input)336         {337             string pattern = @"^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$";338             return IsMatch(input, pattern);339         }340 341         /// <summary>  342         /// 验证电话号码(可以是固定电话号码或手机号码)  343         /// [固定电话:[3位或4位区号;区号可以用小括号括起来;区号可以省略;区号与本地号间可以用减号或空格隔开;可以有3位数的分机号,分机号前要加减号]]  344         /// [手机号码:[可匹配"(+86)013325656352",括号可以省略,+号可以省略,(+86)可以省略,手机号前的0可以省略;手机号第二位数可以是3、4、5、8中的任意一个]]  345         /// </summary>  346         /// <param name="input">待验证的字符串</param>  347         /// <returns>是否匹配</returns>  348         public static bool IsPhoneNumber(string input)349         {350             string pattern = @"^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$|^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$";351             return IsMatch(input, pattern);352         }353 354         /// <summary>  355         /// 验证邮政编码  356         /// </summary>  357         /// <param name="input">待验证的字符串</param>  358         /// <returns>是否匹配</returns>  359         public static bool IsZipCode(string input)360         {361             //string pattern = @"^\d{6}$";  362             //return IsMatch(input, pattern);  363             if (input.Length != 6)364                 return false;365             int i;366             if (int.TryParse(input, out i))367                 return true;368             else369                 return false;370         }371 372         /// <summary>  373         /// 验证电子邮箱  374         /// [@字符前可以包含字母、数字、下划线和点号;@字符后可以包含字母、数字、下划线和点号;@字符后至少包含一个点号且点号不能是最后一个字符;最后一个点号后只能是字母或数字]  375         /// </summary>  376         /// <param name="input">待验证的字符串</param>  377         /// <returns>是否匹配</returns>  378         public static bool IsEmail(string input)379         {380             ////邮箱名以数字或字母开头;邮箱名可由字母、数字、点号、减号、下划线组成;邮箱名(@前的字符)长度为3~18个字符;邮箱名不能以点号、减号或下划线结尾;不能出现连续两个或两个以上的点号、减号。  381             //string pattern = @"^[a-zA-Z0-9]((?<!(\.\.|--))[a-zA-Z0-9\._-]){1,16}[a-zA-Z0-9]@([0-9a-zA-Z][0-9a-zA-Z-]{0,62}\.)+([0-9a-zA-Z][0-9a-zA-Z-]{0,62})\.?|((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$";  382             string pattern = @"^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$";383             return IsMatch(input, pattern);384         }385 386         /// <summary>  387         /// 验证网址(可以匹配IPv4地址但没对IPv4地址进行格式验证;IPv6暂时没做匹配)  388         /// [允许省略"://";可以添加端口号;允许层级;允许传参;域名中至少一个点号且此点号前要有内容]  389         /// </summary>  390         /// <param name="input">待验证的字符串</param>  391         /// <returns>是否匹配</returns>  392         public static bool IsURL(string input)393         {394             ////每级域名由字母、数字和减号构成(第一个字母不能是减号),不区分大小写,单个域长度不超过63,完整的域名全长不超过256个字符。在DNS系统中,全名是以一个点“.”来结束的,例如“www.nit.edu.cn.”。没有最后的那个点则表示一个相对地址。   395             ////没有例如"http://"的前缀,没有传参的匹配  396             //string pattern = @"^([0-9a-zA-Z][0-9a-zA-Z-]{0,62}\.)+([0-9a-zA-Z][0-9a-zA-Z-]{0,62})\.?$";  397 398             //string pattern = @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$";  399             string pattern = @"^([a-zA-Z]+://)?([\w-\.]+)(\.[a-zA-Z0-9]+)(:\d{0,5})?/?([\w-/]*)\.?([a-zA-Z]*)\??(([\w-]*=[\w%]*&?)*)$";400             return IsMatch(input, pattern);401         }402 403         /// <summary>  404         /// 验证IPv4地址  405         /// [第一位和最后一位数字不能是0或255;允许用0补位]  406         /// </summary>  407         /// <param name="input">待验证的字符串</param>  408         /// <returns>是否匹配</returns>  409         public static bool IsIPv4(string input)410         {411             //string pattern = @"^(25[0-4]|2[0-4]\d]|[01]?\d{2}|[1-9])\.(25[0-5]|2[0-4]\d]|[01]?\d?\d)\.(25[0-5]|2[0-4]\d]|[01]?\d?\d)\.(25[0-4]|2[0-4]\d]|[01]?\d{2}|[1-9])$";  412             //return IsMatch(input, pattern);  413             string[] IPs = input.Split(.);414             if (IPs.Length != 4)415                 return false;416             int n = -1;417             for (int i = 0; i < IPs.Length; i++)418             {419                 if (i == 0 || i == 3)420                 {421                     if (int.TryParse(IPs[i], out n) && n > 0 && n < 255)422                         continue;423                     else424                         return false;425                 }426                 else427                 {428                     if (int.TryParse(IPs[i], out n) && n >= 0 && n <= 255)429                         continue;430                     else431                         return false;432                 }433             }434             return true;435         }436 437         /// <summary>  438         /// 验证IPv6地址  439         /// [可用于匹配任何一个合法的IPv6地址]  440         /// </summary>  441         /// <param name="input">待验证的字符串</param>  442         /// <returns>是否匹配</returns>  443         public static bool IsIPv6(string input)444         {445             string pattern = @"^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$";446             return IsMatch(input, pattern);447         }448 449         /// <summary>  450         /// 身份证上数字对应的地址  451         /// </summary>  452         //enum IDAddress  453         //{  454         //    北京 = 11, 天津 = 12, 河北 = 13, 山西 = 14, 内蒙古 = 15, 辽宁 = 21, 吉林 = 22, 黑龙江 = 23, 上海 = 31, 江苏 = 32, 浙江 = 33,  455         //    安徽 = 34, 福建 = 35, 江西 = 36, 山东 = 37, 河南 = 41, 湖北 = 42, 湖南 = 43, 广东 = 44, 广西 = 45, 海南 = 46, 重庆 = 50, 四川 = 51,  456         //    贵州 = 52, 云南 = 53, 西藏 = 54, 陕西 = 61, 甘肃 = 62, 青海 = 63, 宁夏 = 64, 新疆 = 65, 台湾 = 71, 香港 = 81, 澳门 = 82, 国外 = 91  457         //}  458 459         /// <summary>  460         /// 验证一代身份证号(15位数)  461         /// [长度为15位的数字;匹配对应省份地址;生日能正确匹配]  462         /// </summary>  463         /// <param name="input">待验证的字符串</param>  464         /// <returns>是否匹配</returns>  465         public static bool IsIDCard15(string input)466         {467             //验证是否可以转换为15位整数  468             long l = 0;469             if (!long.TryParse(input, out l) || l.ToString().Length != 15)470             {471                 return false;472             }473             //验证省份是否匹配  474             //1~6位为地区代码,其中1、2位数为各省级政府的代码,3、4位数为地、市级政府的代码,5、6位数为县、区级政府代码。  475             string address = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,";476             if (!address.Contains(input.Remove(2) + ","))477             {478                 return false;479             }480             //验证生日是否匹配  481             string birthdate = input.Substring(6, 6).Insert(4, "/").Insert(2, "/");482             DateTime dt;483             if (!DateTime.TryParse(birthdate, out dt))484             {485                 return false;486             }487             return true;488         }489 490         /// <summary>  491         /// 验证二代身份证号(18位数,GB11643-1999标准)  492         /// [长度为18位;前17位为数字,最后一位(校验码)可以为大小写x;匹配对应省份地址;生日能正确匹配;校验码能正确匹配]  493         /// </summary>  494         /// <param name="input">待验证的字符串</param>  495         /// <returns>是否匹配</returns>  496         public static bool IsIDCard18(string input)497         {498             //验证是否可以转换为正确的整数  499             long l = 0;500             if (!long.TryParse(input.Remove(17), out l) || l.ToString().Length != 17 || !long.TryParse(input.Replace(x, 0).Replace(X, 0), out l))501             {502                 return false;503             }504             //验证省份是否匹配  505             //1~6位为地区代码,其中1、2位数为各省级政府的代码,3、4位数为地、市级政府的代码,5、6位数为县、区级政府代码。  506             string address = "11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,";507             if (!address.Contains(input.Remove(2) + ","))508             {509                 return false;510             }511             //验证生日是否匹配  512             string birthdate = input.Substring(6, 8).Insert(6, "/").Insert(4, "/");513             DateTime dt;514             if (!DateTime.TryParse(birthdate, out dt))515             {516                 return false;517             }518             //校验码验证  519             //校验码:  520             //(1)十七位数字本体码加权求和公式   521             //S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和   522             //Ai:表示第i位置上的身份证号码数字值   523             //Wi:表示第i位置上的加权因子   524             //Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2   525             //(2)计算模   526             //Y = mod(S, 11)   527             //(3)通过模得到对应的校验码   528             //Y: 0 1 2 3 4 5 6 7 8 9 10   529             //校验码: 1 0 X 9 8 7 6 5 4 3 2   530             string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(,);531             string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(,);532             char[] Ai = input.Remove(17).ToCharArray();533             int sum = 0;534             for (int i = 0; i < 17; i++)535             {536                 sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());537             }538             int y = -1;539             Math.DivRem(sum, 11, out y);540             if (arrVarifyCode[y] != input.Substring(17, 1).ToLower())541             {542                 return false;543             }544             return true;545         }546 547         /// <summary>  548         /// 验证身份证号(不区分一二代身份证号)  549         /// </summary>  550         /// <param name="input">待验证的字符串</param>  551         /// <returns>是否匹配</returns>  552         public static bool IsIDCard(string input)553         {554             if (input.Length == 18)555                 return IsIDCard18(input);556             else if (input.Length == 15)557                 return IsIDCard15(input);558             else559                 return false;560         }561 562         /// <summary>  563         /// 验证经度  564         /// </summary>  565         /// <param name="input">待验证的字符串</param>  566         /// <returns>是否匹配</returns>  567         public static bool IsLongitude(string input)568         {569             ////范围为-180~180,小数位数必须是1到5位  570             //string pattern = @"^[-\+]?((1[0-7]\d{1}|0?\d{1,2})\.\d{1,5}|180\.0{1,5})$";  571             //return IsMatch(input, pattern);  572             float lon;573             if (float.TryParse(input, out lon) && lon >= -180 && lon <= 180)574                 return true;575             else576                 return false;577         }578 579         /// <summary>  580         /// 验证纬度  581         /// </summary>  582         /// <param name="input">待验证的字符串</param>  583         /// <returns>是否匹配</returns>  584         public static bool IsLatitude(string input)585         {586             ////范围为-90~90,小数位数必须是1到5位  587             //string pattern = @"^[-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$";  588             //return IsMatch(input, pattern);  589             float lat;590             if (float.TryParse(input, out lat) && lat >= -90 && lat <= 90)591                 return true;592             else593                 return false;594         }595         #endregion596     }

 

.net使用正则表达式校验、匹配字符工具类