PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/ElectronicCommerce.Framework.Web/Validation/Extensions/StringValidateExtensions.cs

http://electroniccommerce.codeplex.com
C# | 131 lines | 102 code | 21 blank | 8 comment | 9 complexity | 39889cc3220ab8db55ccf1cf3ee7dd25 MD5 | raw file
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace ElectronicCommerce.Framework.Web.Validation.Extensions
  4. {
  5. public static class StringValidateExtensions
  6. {
  7. public static bool IsNullOrEmpty(this string input)
  8. {
  9. return string.IsNullOrEmpty(input);
  10. }
  11. public static bool IsNumeric(this string input)
  12. {
  13. if (string.IsNullOrEmpty(input))
  14. {
  15. return false;
  16. }
  17. return Regex.IsMatch(input, @"^[0-9]+$");
  18. }
  19. public static bool IsEmailAddress(this string input)
  20. {
  21. if (string.IsNullOrEmpty(input))
  22. {
  23. return false;
  24. }
  25. return Regex.IsMatch(input, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
  26. }
  27. public static bool IsVaildUserName(this string input)
  28. {
  29. if (string.IsNullOrEmpty(input))
  30. {
  31. return false;
  32. }
  33. return Regex.IsMatch(input, @"^[A-Za-z\u4e00-\u9fa5][\w\u4e00-\u9fa5]{4,20}$");
  34. }
  35. public static bool IsDateTime(this string input)
  36. {
  37. if (string.IsNullOrEmpty(input))
  38. {
  39. return false;
  40. }
  41. DateTime result;
  42. return DateTime.TryParse(input, out result);
  43. }
  44. public static bool IsInteger(this string input)
  45. {
  46. if (string.IsNullOrEmpty(input))
  47. {
  48. return false;
  49. }
  50. int result;
  51. return int.TryParse(input, out result);
  52. }
  53. public static bool IsDecimal(this string input)
  54. {
  55. if (string.IsNullOrEmpty(input))
  56. {
  57. return false;
  58. }
  59. return Regex.IsMatch(input, @"^\d+[.]?\d*$");
  60. }
  61. public static bool IsMoney(this string input)
  62. {
  63. if (string.IsNullOrEmpty(input))
  64. {
  65. return false;
  66. }
  67. return Regex.IsMatch(input, "^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$");
  68. }
  69. public static bool IsURL(this string input)
  70. {
  71. if (string.IsNullOrEmpty(input))
  72. {
  73. return false;
  74. }
  75. return Regex.IsMatch(input, @"(mailto\:|(news|(ht|f)tp(s?))\://)(([^[:space:]]+)|([^[:space:]]+)( #([^#]+)#)?) ");
  76. }
  77. public static bool IsChineseMobileNumber(this string input)
  78. {
  79. if (string.IsNullOrEmpty(input))
  80. {
  81. return false;
  82. }
  83. return Regex.IsMatch(input, @"^(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$");
  84. }
  85. public static bool IsChinesePhoneNumber(this string input)
  86. {
  87. return Regex.IsMatch(input, @"^[0-9\-]{0,20}$");
  88. }
  89. /// <summary>
  90. /// ?????
  91. /// 11?????
  92. /// 3-4????7-8??????1?4????
  93. /// ??12345678901?1234-12345678-1234
  94. /// </summary>
  95. /// <param name="input"></param>
  96. /// <returns></returns>
  97. public static bool IsChinesePhoneNumber2(this string input)
  98. {
  99. return Regex.IsMatch(input, @"((\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}))$)");
  100. }
  101. public static bool IsIPV6Address(this string input)
  102. {
  103. return Regex.IsMatch(input, @"^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}$");
  104. }
  105. public static bool IsIPV4Address(this string input)
  106. {
  107. return Regex.IsMatch(input, @"^((?:2[0-5]{2}|1\d{2}|[1-9]\d|[1-9])\.(?:(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)\.){2}(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)):(\d|[1-9]\d|[1-9]\d{2,3}|[1-5]\d{4}|6[0-4]\d{3}|654\d{2}|655[0-2]\d|6553[0-5])$");
  108. }
  109. }
  110. }