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