verify.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * 用户输入内容验证类
  3. */
  4. // 是否为空
  5. export const isEmpty = (str) => {
  6. return str.trim() == ''
  7. }
  8. /**
  9. * 匹配phone
  10. */
  11. export const isPhone = (str) => {
  12. const reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/
  13. return reg.test(str)
  14. }
  15. /**
  16. * 匹配phone
  17. */
  18. export const isMobile = (str) => {
  19. const reg = /^(1[3456789]\d{9})$/
  20. return reg.test(str)
  21. }
  22. /**
  23. * 匹配Email地址
  24. */
  25. export const isEmail = (str) => {
  26. if (str == null || str == "") return false
  27. var result = str.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)
  28. if (result == null) return false
  29. return true
  30. }
  31. /**
  32. * 判断数值类型,包括整数和浮点数
  33. */
  34. export const isNumber = (str) => {
  35. if (isDouble(str) || isInteger(str)) return true
  36. return false
  37. }
  38. /**
  39. * 判断是否为正整数(只能输入数字[0-9])
  40. */
  41. export const isPositiveInteger = (str) => {
  42. return /(^[0-9]\d*$)/.test(str)
  43. }
  44. /**
  45. * 匹配integer
  46. */
  47. export const isInteger = (str) => {
  48. if (str == null || str == "") return false
  49. var result = str.match(/^[-\+]?\d+$/)
  50. if (result == null) return false
  51. return true
  52. }
  53. /**
  54. * 匹配double或float
  55. */
  56. export const isDouble = (str) => {
  57. if (str == null || str == "") return false
  58. var result = str.match(/^[-\+]?\d+(\.\d+)?$/)
  59. if (result == null) return false
  60. return true
  61. }
  62. // 判断的是否是JSON字符串
  63. export const isJson = (str) => {
  64. if (typeof str == 'string') {
  65. try {
  66. var obj = JSON.parse(str);
  67. // 等于这个条件说明就是JSON字符串 会返回true
  68. if (typeof obj == 'object' && obj) {
  69. return true;
  70. } else {
  71. //不是就返回false
  72. return false;
  73. }
  74. } catch (e) {
  75. return false;
  76. }
  77. }
  78. return false;
  79. }
  80. /**
  81. * 根据身份证获取出生年月日
  82. * @param {String} IdNO - 身份证号
  83. * @return {Number} 日期
  84. */
  85. export const getBirthdayByIdNO = (IdNO) => {
  86. let birthday = "";
  87. if (IdNO.length == 18) {
  88. birthday = IdNO.substr(6, 8);
  89. return birthday.replace(/(.{4})(.{2})/, "$1-$2-");
  90. } else if (IdNO.length == 15) {
  91. birthday = "19" + IdNO.substr(6, 6);
  92. return birthday.replace(/(.{4})(.{2})/, "$1-$2-");
  93. } else {
  94. return "";
  95. }
  96. }
  97. /**
  98. * 根据身份证获取出性别
  99. * @param {String} IdNO - 身份证号
  100. * @return {Number} 0 女 , 1 男 ,未识别成功 男
  101. */
  102. export const getSexByIdNO = (IdNO) => {
  103. if (IdNO.length == 18) {
  104. return IdNO.charAt(16) % 2 == 0 ? '0' : '1';
  105. } else if (IdNO.length == 15) {
  106. return IdNO.charAt(14) % 2 == 0 ? '0' : '1';
  107. } else {
  108. return '1';
  109. }
  110. }