C# 常用正则验证[转]
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Text.RegularExpressions; 7 8 namespace Common 9 { 10 public class Validate 11 { 12 private static readonly Regex RegPhone = new Regex("(^(\\d{11})$|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)"); //电话号码和手机验证 13 private static Regex RegEmail = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@([\\w-]+\\.)+\\w{2,3})\\s*$"); //new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 14 private static Regex RegNum = new Regex("^[0-9]+$"); 15 16 17 //必须是数字正则表达式(小数或整数) 18 private static Regex regex = new Regex(@"/^[0-9]+\.?[0-9]{0,3}$/"); 19 /// <summary> 20 /// 身份证正值表达式 21 /// </summary> 22 private static readonly Regex RegCardId = new Regex("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)"); 23 24 #region 确定用户输入是否合法 25 /// <summary> 26 /// 确定用户输入是否合法 27 /// </summary> 28 /// <param name="text">用户输入字符串</param> 29 /// <param name="maxLength">最大字符串长度</param> 30 /// <returns></returns> 31 public static string InputText(string text, int maxLength) 32 { 33 if (string.IsNullOrEmpty(text)) 34 return string.Empty; 35 text = text.Trim(); 36 if (maxLength != 0) 37 if (text.Length > maxLength) 38 text = text.Substring(0, maxLength); 39 text = Regex.Replace(text, "[\\s]{2,}", " "); 40 text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); 41 text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); 42 //text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //屏蔽标签 43 text = text.Replace("'", "''"); 44 return text; 45 } 46 #endregion 47 48 49 #region 验证电话号码 50 // 电话号码和手机号码检查 51 /// <summary> 52 /// 电话号码和手机号码检查 53 /// </summary> 54 /// <param name="inputData">电话号码或手机号码</param> 55 /// <returns>匹配结果</returns> 56 public static bool IsPhone(string inputData) 57 { 58 Match m = RegPhone.Match(inputData); 59 return m.Success; 60 } 61 #endregion 62 63 64 #region 验证参数是否为中文字符 65 /// <summary> 66 /// 验证参数是否为中文字符 67 /// </summary> 68 /// <param name="input">输入参数</param> 69 /// <returns></returns> 70 public static bool IsChinese(string input) 71 { 72 Regex regex = new Regex(@"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase); 73 return regex.IsMatch(input); 74 } 75 #endregion 76 77 78 #region 邮件地址 79 80 81 /// <summary> 82 /// 邮件地址验证 83 /// </summary> 84 /// <param name="inputData">输入字符串</param> 85 /// <returns>验证结果</returns> 86 public static bool IsEmail(string inputData) 87 { 88 Match m = RegEmail.Match(inputData); 89 return m.Success; 90 } 91 #endregion 92 93 94 #region 是否为数字 95 /// <summary> 96 /// 是否为数字 97 /// </summary> 98 /// <param name="inputData">输入字符串</param> 99 /// <returns>是否为数字</returns> 100 public static bool IsNum(string inputData) 101 { 102 if (string.IsNullOrEmpty(inputData)) 103 { 104 return false; 105 } 106 Match m = RegNum.Match(inputData); 107 return m.Success; 108 } 109 /// <summary> 110 /// 判断是否是整数或小数 111 /// </summary> 112 /// <param name="str">输入的字符串</param> 113 /// <returns>是否为数字</returns> 114 public static bool IsNumAll(string str) 115 { 116 if (string.IsNullOrEmpty(str)) 117 { 118 return false; 119 } 120 Match m = regex.Match(str); 121 return m.Success; 122 } 123 124 125 #endregion 126 127 128 #region 是否为身份证 129 /// <summary> 130 /// 是否为身份证 131 /// </summary> 132 /// <param name="inputData">输入字符串</param> 133 /// <returns>是否为身份证</returns> 134 public static bool IsCardId(string inputData) 135 { 136 Match m = RegCardId.Match(inputData); 137 return m.Success; 138 } 139 #endregion 140 141 142 /// <summary> 143 /// 判断字符串是否是纯数字 144 /// </summary> 145 /// <param name="message">源字符串</param> 146 /// <returns></returns> 147 public static bool IsNumberic(string message)//, out int result 148 { 149 System.Text.RegularExpressions.Regex rex = 150 new System.Text.RegularExpressions.Regex(@"^\d+$"); 151 var result = -1; 152 if (rex.IsMatch(message)) 153 { 154 result = int.Parse(message); 155 return true; 156 } 157 else 158 return false; 159 } 160 161 162 #region 匹配方法 163 /// <summary> 164 /// 验证字符串是否匹配正则表达式描述的规则 165 /// </summary> 166 /// <param name="inputStr">待验证的字符串</param> 167 /// <param name="patternStr">正则表达式字符串</param> 168 /// <returns>是否匹配</returns> 169 public static bool IsMatch(string inputStr, string patternStr) 170 { 171 return IsMatch(inputStr, patternStr, false, false); 172 } 173 174 /// <summary> 175 /// 验证字符串是否匹配正则表达式描述的规则 176 /// </summary> 177 /// <param name="inputStr">待验证的字符串</param> 178 /// <param name="patternStr">正则表达式字符串</param> 179 /// <param name="ifIgnoreCase">匹配时是否不区分大小写</param> 180 /// <returns>是否匹配</returns> 181 public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase) 182 { 183 return IsMatch(inputStr, patternStr, ifIgnoreCase, false); 184 } 185 186 /// <summary> 187 /// 验证字符串是否匹配正则表达式描述的规则 188 /// </summary> 189 /// <param name="inputStr">待验证的字符串</param> 190 /// <param name="patternStr">正则表达式字符串</param> 191 /// <param name="ifIgnoreCase">匹配时是否不区分大小写</param> 192 /// <param name="ifValidateWhiteSpace">是否验证空白字符串</param> 193 /// <returns>是否匹配</returns> 194 public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase, bool ifValidateWhiteSpace) 195 { 196 if (!ifValidateWhiteSpace && string.IsNullOrWhiteSpace(inputStr))//.NET 4.0 新增IsNullOrWhiteSpace 方法,便于对用户做处理 197 return false;//如果不要求验证空白字符串而此时传入的待验证字符串为空白字符串,则不匹配 198 Regex regex = null; 199 if (ifIgnoreCase) 200 regex = new Regex(patternStr, RegexOptions.IgnoreCase);//指定不区分大小写的匹配 201 else 202 regex = new Regex(patternStr); 203 return regex.IsMatch(inputStr); 204 } 205 #endregion 206 207 #region 验证方法 208 /// <summary> 209 /// 验证数字(double类型) 210 /// [可以包含负号和小数点] 211 /// </summary> 212 /// <param name="input">待验证的字符串</param> 213 /// <returns>是否匹配</returns> 214 public static bool IsNumber(string input) 215 { 216 //string pattern = @"^-?\d+$|^(-?\d+)(\.\d+)?$"; 217 //return IsMatch(input, pattern); 218 double d = 0; 219 if (double.TryParse(input, out d)) 220 return true; 221 else 222 return false; 223 } 224 225 /// <summary> 226 /// 验证整数 227 /// </summary> 228 /// <param name="input">待验证的字符串</param> 229 /// <returns>是否匹配</returns> 230 public static bool IsInteger(string input) 231 { 232 //string pattern = @"^-?\d+$"; 233 //return IsMatch(input, pattern); 234 int i = 0; 235 if (int.TryParse(input, out i)) 236 return true; 237 else 238 return false; 239 } 240 241 /// <summary> 242 /// 验证非负整数 243 /// </summary> 244 /// <param name="input">待验证的字符串</param> 245 /// <returns>是否匹配</returns> 246 public static bool IsIntegerNotNagtive(string input) 247 { 248 //string pattern = @"^\d+$"; 249 //return IsMatch(input, pattern); 250 int i = -1; 251 if (int.TryParse(input, out i) && i >= 0) 252 return true; 253 else 254 return false; 255 } 256 257 /// <summary> 258 /// 验证正整数 259 /// </summary> 260 /// <param name="input">待验证的字符串</param> 261 /// <returns>是否匹配</returns> 262 public static bool IsIntegerPositive(string input) 263 { 264 //string pattern = @"^[0-9]*[1-9][0-9]*$"; 265 //return IsMatch(input, pattern); 266 int i = 0; 267 if (int.TryParse(input, out i) && i >= 1) 268 return true; 269 else 270 return false; 271 } 272 273 /// <summary> 274 /// 验证小数 275 /// </summary> 276 /// <param name="input">待验证的字符串</param> 277 /// <returns>是否匹配</returns> 278 public static bool IsDecimal(string input) 279 { 280 string pattern = @"^([-+]?[1-9]\d*\.\d+|-?0\.\d*[1-9]\d*)$"; 281 return IsMatch(input, pattern); 282 } 283 284 /// <summary> 285 /// 验证只包含英文字母 286 /// </summary> 287 /// <param name="input">待验证的字符串</param> 288 /// <returns>是否匹配</returns> 289 public static bool IsEnglishCharacter(string input) 290 { 291 string pattern = @"^[A-Za-z]+$"; 292 return IsMatch(input, pattern); 293 } 294 295 /// <summary> 296 /// 验证只包含数字和英文字母 297 /// </summary> 298 /// <param name="input">待验证的字符串</param> 299 /// <returns>是否匹配</returns> 300 public static bool IsIntegerAndEnglishCharacter(string input) 301 { 302 string pattern = @"^[0-9A-Za-z]+$"; 303 return IsMatch(input, pattern); 304 } 305 306 /// <summary> 307 /// 验证只包含汉字 308 /// </summary> 309 /// <param name="input">待验证的字符串</param> 310 /// <returns>是否匹配</returns> 311 public static bool IsChineseCharacter(string input) 312 { 313 string pattern = @"^[\u4e00-\u9fa5]+$"; 314 return IsMatch(input, pattern); 315 } 316 317 /// <summary> 318 /// 验证数字长度范围(数字前端的0计长度) 319 /// [若要验证固定长度,可传入相同的两个长度数值] 320 /// </summary> 321 /// <param name="input">待验证的字符串</param> 322 /// <param name="lengthBegin">长度范围起始值(含)</param> 323 /// <param name="lengthEnd">长度范围结束值(含)</param> 324 /// <returns>是否匹配</returns> 325 public static bool IsIntegerLength(string input, int lengthBegin, int lengthEnd) 326 { 327 //string pattern = @"^\d{" + lengthBegin + "," + lengthEnd + "}$"; 328 //return IsMatch(input, pattern); 329 if (input.Length >= lengthBegin && input.Length <= lengthEnd) 330 { 331 int i; 332 if (int.TryParse(input, out i)) 333 return true; 334 else 335 return false; 336 } 337 else 338 return false; 339 } 340 341 /// <summary> 342 /// 验证字符串包含内容 343 /// </summary> 344 /// <param name="input">待验证的字符串</param> 345 /// <param name="withEnglishCharacter">是否包含英文字母</param> 346 /// <param name="withNumber">是否包含数字</param> 347 /// <param name="withChineseCharacter">是否包含汉字</param> 348 /// <returns>是否匹配</returns> 349 public static bool IsStringInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter) 350 { 351 if (!withEnglishCharacter && !withNumber && !withChineseCharacter) 352 return false;//如果英文字母、数字和汉字都没有,则返回false 353 StringBuilder patternString = new StringBuilder(); 354 patternString.Append("^["); 355 if (withEnglishCharacter) 356 patternString.Append("a-zA-Z"); 357 if (withNumber) 358 patternString.Append("0-9"); 359 if (withChineseCharacter) 360 patternString.Append(@"\u4E00-\u9FA5"); 361 patternString.Append("]+$"); 362 return IsMatch(input, patternString.ToString()); 363 } 364 365 /// <summary> 366 /// 验证字符串长度范围 367 /// [若要验证固定长度,可传入相同的两个长度数值] 368 /// </summary> 369 /// <param name="input">待验证的字符串</param> 370 /// <param name="lengthBegin">长度范围起始值(含)</param> 371 /// <param name="lengthEnd">长度范围结束值(含)</param> 372 /// <returns>是否匹配</returns> 373 public static bool IsStringLength(string input, int lengthBegin, int lengthEnd) 374 { 375 //string pattern = @"^.{" + lengthBegin + "," + lengthEnd + "}$"; 376 //return IsMatch(input, pattern); 377 if (input.Length >= lengthBegin && input.Length <= lengthEnd) 378 return true; 379 else 380 return false; 381 } 382 383 /// <summary> 384 /// 验证字符串长度范围(字符串内只包含数字和/或英文字母) 385 /// [若要验证固定长度,可传入相同的两个长度数值] 386 /// </summary> 387 /// <param name="input">待验证的字符串</param> 388 /// <param name="lengthBegin">长度范围起始值(含)</param> 389 /// <param name="lengthEnd">长度范围结束值(含)</param> 390 /// <returns>是否匹配</returns> 391 public static bool IsStringLengthOnlyNumberAndEnglishCharacter(string input, int lengthBegin, int lengthEnd) 392 { 393 string pattern = @"^[0-9a-zA-z]{" + lengthBegin + "," + lengthEnd + "}$"; 394 return IsMatch(input, pattern); 395 } 396 397 /// <summary> 398 /// 验证字符串长度范围 399 /// [若要验证固定长度,可传入相同的两个长度数值] 400 /// </summary> 401 /// <param name="input">待验证的字符串</param> 402 /// <param name="withEnglishCharacter">是否包含英文字母</param> 403 /// <param name="withNumber">是否包含数字</param> 404 /// <param name="withChineseCharacter">是否包含汉字</param> 405 /// <param name="lengthBegin">长度范围起始值(含)</param> 406 /// <param name="lengthEnd">长度范围结束值(含)</param> 407 /// <returns>是否匹配</returns> 408 public static bool IsStringLengthByInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter, int lengthBegin, int lengthEnd) 409 { 410 if (!withEnglishCharacter && !withNumber && !withChineseCharacter) 411 return false;//如果英文字母、数字和汉字都没有,则返回false 412 StringBuilder patternString = new StringBuilder(); 413 patternString.Append("^["); 414 if (withEnglishCharacter) 415 patternString.Append("a-zA-Z"); 416 if (withNumber) 417 patternString.Append("0-9"); 418 if (withChineseCharacter) 419 patternString.Append(@"\u4E00-\u9FA5"); 420 patternString.Append("]{" + lengthBegin + "," + lengthEnd + "}$"); 421 return IsMatch(input, patternString.ToString()); 422 } 423 424 /// <summary> 425 /// 验证字符串字节数长度范围 426 /// [若要验证固定长度,可传入相同的两个长度数值;每个汉字为两个字节长度] 427 /// </summary> 428 /// <param name="input">待验证的字符串</param> 429 /// <param name="lengthBegin">长度范围起始值(含)</param> 430 /// <param name="lengthEnd">长度范围结束值(含)</param> 431 /// <returns></returns> 432 public static bool IsStringByteLength(string input, int lengthBegin, int lengthEnd) 433 { 434 //int byteLength = Regex.Replace(input, @"[^\x00-\xff]", "ok").Length; 435 //if (byteLength >= lengthBegin && byteLength <= lengthEnd) 436 //{ 437 // return true; 438 //} 439 //return false; 440 int byteLength = Encoding.Default.GetByteCount(input); 441 if (byteLength >= lengthBegin && byteLength <= lengthEnd) 442 return true; 443 else 444 return false; 445 } 446 447 /// <summary> 448 /// 验证日期 449 /// </summary> 450 /// <param name="input">待验证的字符串</param> 451 /// <returns>是否匹配</returns> 452 public static bool IsDateTime(string input) 453 { 454 DateTime dt; 455 if (DateTime.TryParse(input, out dt)) 456 return true; 457 else 458 return false; 459 } 460 461 /// <summary> 462 /// 验证固定电话号码 463 /// [3位或4位区号;区号可以用小括号括起来;区号可以省略;区号与本地号间可以用减号或空格隔开;可以有3位数的分机号,分机号前要加减号] 464 /// </summary> 465 /// <param name="input">待验证的字符串</param> 466 /// <returns>是否匹配</returns> 467 public static bool IsTelePhoneNumber(string input) 468 { 469 string pattern = @"^(((0\d2|0\d{2})[- ]?)?\d{8}|((0\d3|0\d{3})[- ]?)?\d{7})(-\d{3})?$"; 470 return IsMatch(input, pattern); 471 } 472 473 /// <summary> 474 /// 验证手机号码 475 /// [可匹配"(+86)013325656352",括号可以省略,+号可以省略,(+86)可以省略,11位手机号前的0可以省略;11位手机号第二位数可以是3、4、5、8中的任意一个] 476 /// </summary> 477 /// <param name="input">待验证的字符串</param> 478 /// <returns>是否匹配</returns> 479 public static bool IsMobilePhoneNumber(string input) 480 { 481 string pattern = @"^((\+)?86|((\+)?86)?)0?1[3458]\d{9}$"; 482 return IsMatch(input, pattern); 483 } 484 485 /// <summary> 486 /// 验证电话号码(可以是固定电话号码或手机号码) 487 /// [固定电话:[3位或4位区号;区号可以用小括号括起来;区号可以省略;区号与本地号间可以用减号或空格隔开;可以有3位数的分机号,分机号前要加减号]] 488 /// [手机号码:[可匹配"(+86)013325656352",括号可以省略,+号可以省略,(+86)可以省略,手机号前的0可以省略;手机号第二位数可以是3、4、5、8中的任意一个]] 489 /// </summary> 490 /// <param name="input">待验证的字符串</param> 491 /// <returns>是否匹配</returns> 492 public static bool IsPhoneNumber(string input) 493 { 494 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})?$"; 495 return IsMatch(input, pattern); 496 } 497 498 /// <summary> 499 /// 验证邮政编码 500 /// </summary> 501 /// <param name="input">待验证的字符串</param> 502 /// <returns>是否匹配</returns> 503 public static bool IsZipCode(string input) 504 { 505 //string pattern = @"^\d{6}$"; 506 //return IsMatch(input, pattern); 507 if (input.Length != 6) 508 return false; 509 int i; 510 if (int.TryParse(input, out i)) 511 return true; 512 else 513 return false; 514 } 515 516 /// <summary> 517 /// 验证电子邮箱 518 /// [@字符前可以包含字母、数字、下划线和点号;@字符后可以包含字母、数字、下划线和点号;@字符后至少包含一个点号且点号不能是最后一个字符;最后一个点号后只能是字母或数字] 519 /// </summary> 520 /// <param name="input">待验证的字符串</param> 521 /// <returns>是否匹配</returns> 522 public static bool IsEmail1(string input) 523 { 524 ////邮箱名以数字或字母开头;邮箱名可由字母、数字、点号、减号、下划线组成;邮箱名(@前的字符)长度为3~18个字符;邮箱名不能以点号、减号或下划线结尾;不能出现连续两个或两个以上的点号、减号。 525 //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?)$"; 526 string pattern = @"^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$"; 527 return IsMatch(input, pattern); 528 } 529 530 /// <summary> 531 /// 验证网址(可以匹配IPv4地址但没对IPv4地址进行格式验证;IPv6暂时没做匹配) 532 /// [允许省略"://";可以添加端口号;允许层级;允许传参;域名中至少一个点号且此点号前要有内容] 533 /// </summary> 534 /// <param name="input">待验证的字符串</param> 535 /// <returns>是否匹配</returns> 536 public static bool IsURL(string input) 537 { 538 ////每级域名由字母、数字和减号构成(第一个字母不能是减号),不区分大小写,单个域长度不超过63,完整的域名全长不超过256个字符。在DNS系统中,全名是以一个点“.”来结束的,例如“www.nit.edu.cn.”。没有最后的那个点则表示一个相对地址。 539 ////没有例如"http://"的前缀,没有传参的匹配 540 //string pattern = @"^([0-9a-zA-Z][0-9a-zA-Z-]{0,62}\.)+([0-9a-zA-Z][0-9a-zA-Z-]{0,62})\.?$"; 541 542 //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\&%_\./-~-]*)?$"; 543 string pattern = @"^([a-zA-Z]+://)?([\w-\.]+)(\.[a-zA-Z0-9]+)(:\d{0,5})?/?([\w-/]*)\.?([a-zA-Z]*)\??(([\w-]*=[\w%]*&?)*)$"; 544 return IsMatch(input, pattern); 545 } 546 547 /// <summary> 548 /// 验证IPv4地址 549 /// [第一位和最后一位数字不能是0或255;允许用0补位] 550 /// </summary> 551 /// <param name="input">待验证的字符串</param> 552 /// <returns>是否匹配</returns> 553 public static bool IsIPv4(string input) 554 { 555 //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])$"; 556 //return IsMatch(input, pattern); 557 string[] IPs = input.Split('.'); 558 if (IPs.Length != 4) 559 return false; 560 int n = -1; 561 for (int i = 0; i < IPs.Length; i++) 562 { 563 if (i == 0 || i == 3) 564 { 565 if (int.TryParse(IPs[i], out n) && n > 0 && n < 255) 566 continue; 567 else 568 return false; 569 } 570 else 571 { 572 if (int.TryParse(IPs[i], out n) && n >= 0 && n <= 255) 573 continue; 574 else 575 return false; 576 } 577 } 578 return true; 579 } 580 581 /// <summary> 582 /// 验证IPv6地址 583 /// [可用于匹配任何一个合法的IPv6地址] 584 /// </summary> 585 /// <param name="input">待验证的字符串</param> 586 /// <returns>是否匹配</returns> 587 public static bool IsIPv6(string input) 588 { 589 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*$"; 590 return IsMatch(input, pattern); 591 } 592 593 /// <summary> 594 /// 身份证上数字对应的地址 595 /// </summary> 596 //enum IDAddress 597 //{ 598 // 北京 = 11, 天津 = 12, 河北 = 13, 山西 = 14, 内蒙古 = 15, 辽宁 = 21, 吉林 = 22, 黑龙江 = 23, 上海 = 31, 江苏 = 32, 浙江 = 33, 599 // 安徽 = 34, 福建 = 35, 江西 = 36, 山东 = 37, 河南 = 41, 湖北 = 42, 湖南 = 43, 广东 = 44, 广西 = 45, 海南 = 46, 重庆 = 50, 四川 = 51, 600 // 贵州 = 52, 云南 = 53, 西藏 = 54, 陕西 = 61, 甘肃 = 62, 青海 = 63, 宁夏 = 64, 新疆 = 65, 台湾 = 71, 香港 = 81, 澳门 = 82, 国外 = 91 601 //} 602 603 /// <summary> 604 /// 验证一代身份证号(15位数) 605 /// [长度为15位的数字;匹配对应省份地址;生日能正确匹配] 606 /// </summary> 607 /// <param name="input">待验证的字符串</param> 608 /// <returns>是否匹配</returns> 609 public static bool IsIDCard15(string input) 610 { 611 //验证是否可以转换为15位整数 612 long l = 0; 613 if (!long.TryParse(input, out l) || l.ToString().Length != 15) 614 { 615 return false; 616 } 617 //验证省份是否匹配 618 //1~6位为地区代码,其中1、2位数为各省级政府的代码,3、4位数为地、市级政府的代码,5、6位数为县、区级政府代码。 619 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,"; 620 if (!address.Contains(input.Remove(2) + ",")) 621 { 622 return false; 623 } 624 //验证生日是否匹配 625 string birthdate = input.Substring(6, 6).Insert(4, "/").Insert(2, "/"); 626 DateTime dt; 627 if (!DateTime.TryParse(birthdate, out dt)) 628 { 629 return false; 630 } 631 return true; 632 } 633 634 /// <summary> 635 /// 验证二代身份证号(18位数,GB11643-1999标准) 636 /// [长度为18位;前17位为数字,最后一位(校验码)可以为大小写x;匹配对应省份地址;生日能正确匹配;校验码能正确匹配] 637 /// </summary> 638 /// <param name="input">待验证的字符串</param> 639 /// <returns>是否匹配</returns> 640 public static bool IsIDCard18(string input) 641 { 642 //验证是否可以转换为正确的整数 643 long l = 0; 644 if (!long.TryParse(input.Remove(17), out l) || l.ToString().Length != 17 || !long.TryParse(input.Replace('x', '0').Replace('X', '0'), out l)) 645 { 646 return false; 647 } 648 //验证省份是否匹配 649 //1~6位为地区代码,其中1、2位数为各省级政府的代码,3、4位数为地、市级政府的代码,5、6位数为县、区级政府代码。 650 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,"; 651 if (!address.Contains(input.Remove(2) + ",")) 652 { 653 return false; 654 } 655 //验证生日是否匹配 656 string birthdate = input.Substring(6, 8).Insert(6, "/").Insert(4, "/"); 657 DateTime dt; 658 if (!DateTime.TryParse(birthdate, out dt)) 659 { 660 return false; 661 } 662 //校验码验证 663 //校验码: 664 //(1)十七位数字本体码加权求和公式 665 //S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和 666 //Ai:表示第i位置上的身份证号码数字值 667 //Wi:表示第i位置上的加权因子 668 //Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 669 //(2)计算模 670 //Y = mod(S, 11) 671 //(3)通过模得到对应的校验码 672 //Y: 0 1 2 3 4 5 6 7 8 9 10 673 //校验码: 1 0 X 9 8 7 6 5 4 3 2 674 string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(','); 675 string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(','); 676 char[] Ai = input.Remove(17).ToCharArray(); 677 int sum = 0; 678 for (int i = 0; i < 17; i++) 679 { 680 sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString()); 681 } 682 int y = -1; 683 Math.DivRem(sum, 11, out y); 684 if (arrVarifyCode[y] != input.Substring(17, 1).ToLower()) 685 { 686 return false; 687 } 688 return true; 689 } 690 691 /// <summary> 692 /// 验证身份证号(不区分一二代身份证号) 693 /// </summary> 694 /// <param name="input">待验证的字符串</param> 695 /// <returns>是否匹配</returns> 696 public static bool IsIDCard(string input) 697 { 698 if (input.Length == 18) 699 return IsIDCard18(input); 700 else if (input.Length == 15) 701 return IsIDCard15(input); 702 else 703 return false; 704 } 705 706 /// <summary> 707 /// 验证经度 708 /// </summary> 709 /// <param name="input">待验证的字符串</param> 710 /// <returns>是否匹配</returns> 711 public static bool IsLongitude(string input) 712 { 713 ////范围为-180~180,小数位数必须是1到5位 714 //string pattern = @"^[-\+]?((1[0-7]\d{1}|0?\d{1,2})\.\d{1,5}|180\.0{1,5})$"; 715 //return IsMatch(input, pattern); 716 float lon; 717 if (float.TryParse(input, out lon) && lon >= -180 && lon <= 180) 718 return true; 719 else 720 return false; 721 } 722 723 /// <summary> 724 /// 验证纬度 725 /// </summary> 726 /// <param name="input">待验证的字符串</param> 727 /// <returns>是否匹配</returns> 728 public static bool IsLatitude(string input) 729 { 730 ////范围为-90~90,小数位数必须是1到5位 731 //string pattern = @"^[-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$"; 732 //return IsMatch(input, pattern); 733 float lat; 734 if (float.TryParse(input, out lat) && lat >= -90 && lat <= 90) 735 return true; 736 else 737 return false; 738 } 739 #endregion 740 } 741 }
如果有错误的地方,还望各位多多指点
写个博客,来记录自己成长的一些经历,或许也能顺便帮助他人。
由于使用GitHub仓库作为图床,会有图片显示不出来的情况。