Validator.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace CB.Common
  6. {
  7. public class Validator
  8. {
  9. /// <summary>
  10. /// 判断对象是否为Int32类型的数字
  11. /// </summary>
  12. /// <param name="Expression"></param>
  13. /// <returns></returns>
  14. public static bool IsNumeric(object expression)
  15. {
  16. if (expression != null)
  17. return IsNumeric(expression.ToString());
  18. return false;
  19. }
  20. /// <summary>
  21. /// 判断对象是否为Int32类型的数字
  22. /// </summary>
  23. /// <param name="Expression"></param>
  24. /// <returns></returns>
  25. public static bool IsNumeric(string expression)
  26. {
  27. if (expression != null)
  28. {
  29. string str = expression;
  30. if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
  31. {
  32. if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. /// <summary>
  39. /// 是否为Double类型
  40. /// </summary>
  41. /// <param name="expression"></param>
  42. /// <returns></returns>
  43. public static bool IsDouble(object expression)
  44. {
  45. if (expression != null)
  46. return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
  47. return false;
  48. }
  49. /// <summary>
  50. /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
  51. /// </summary>
  52. /// <param name="strNumber">要确认的字符串数组</param>
  53. /// <returns>是则返加true 不是则返回 false</returns>
  54. public static bool IsNumericArray(string[] strNumber)
  55. {
  56. if (strNumber == null)
  57. return false;
  58. if (strNumber.Length < 1)
  59. return false;
  60. foreach (string id in strNumber)
  61. {
  62. if (!IsNumeric(id))
  63. return false;
  64. }
  65. return true;
  66. }
  67. }
  68. }