util.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //身份证号校验
  2. const isCardID = sId => {
  3. if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
  4. return {
  5. data: "你输入的身份证长度或格式有误",
  6. state: false
  7. };
  8. }
  9. //身份证城市
  10. var aCity = {
  11. 11: "北京",
  12. 12: "天津",
  13. 13: "河北",
  14. 14: "山西",
  15. 15: "内蒙古",
  16. 21: "辽宁",
  17. 22: "吉林",
  18. 23: "黑龙江",
  19. 31: "上海",
  20. 32: "江苏",
  21. 33: "浙江",
  22. 34: "安徽",
  23. 35: "福建",
  24. 36: "江西",
  25. 37: "山东",
  26. 41: "河南",
  27. 42: "湖北",
  28. 43: "湖南",
  29. 44: "广东",
  30. 45: "广西",
  31. 46: "海南",
  32. 50: "重庆",
  33. 51: "四川",
  34. 52: "贵州",
  35. 53: "云南",
  36. 54: "西藏",
  37. 61: "陕西",
  38. 62: "甘肃",
  39. 63: "青海",
  40. 64: "宁夏",
  41. 65: "新疆",
  42. 71: "台湾",
  43. 81: "香港",
  44. 82: "澳门",
  45. 91: "国外"
  46. };
  47. if (!aCity[parseInt(sId.substr(0, 2))]) {
  48. return {
  49. data: "你的身份证地区有误",
  50. state: false
  51. };
  52. }
  53. // 出生日期验证
  54. var sBirthday = (
  55. sId.substr(6, 4) +
  56. "-" +
  57. Number(sId.substr(10, 2)) +
  58. "-" +
  59. Number(sId.substr(12, 2))
  60. ).replace(/-/g, "/"),
  61. d = new Date(sBirthday);
  62. if (
  63. sBirthday !=
  64. d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate()
  65. ) {
  66. return {
  67. data: "身份证上的出生日期有误",
  68. state: false
  69. };
  70. }
  71. // 身份证号码校验
  72. var sum = 0,
  73. weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
  74. codes = "10X98765432";
  75. for (var i = 0; i < sId.length - 1; i++) {
  76. sum += sId[i] * weights[i];
  77. }
  78. var last = codes[sum % 11]; //计算出来的最后一位身份证号码
  79. if (sId[sId.length - 1] != last) {
  80. return {
  81. data: "你输入的身份证号长度或格式有误",
  82. state: false
  83. };
  84. }
  85. return {
  86. data: "",
  87. state: true
  88. };
  89. };
  90. //手机号校验
  91. const isPhone = phone => {
  92. if (!/^1[3-9]\d{9}$/.test(phone)) {
  93. return {
  94. data: "手机号只能由11位数字组成",
  95. state: false
  96. };
  97. } else {
  98. return {
  99. data: "",
  100. state: true
  101. };
  102. }
  103. };
  104. //短号校验
  105. const isCornet = sId => {
  106. if (!/^(?!.*-.*-)[-\d]{3,11}$/.test(sId)) {
  107. return {
  108. data: "长度或格式未达标",
  109. state: false
  110. };
  111. } else {
  112. return {
  113. data: "",
  114. state: true
  115. };
  116. }
  117. };
  118. //中文字符校验
  119. const isChineseWord = str => {
  120. if (!/^[\u4e00-\u9fa5]*$/.test(str)) {
  121. return {
  122. data: "姓名只能由中文组成",
  123. state: false
  124. };
  125. } else {
  126. return {
  127. data: "",
  128. state: true
  129. };
  130. }
  131. };
  132. //获得对应时间格式
  133. const formatTime = (time, type) => {
  134. var date = new Date(time);
  135. var format = type || "YYYY-MM-DD HH:NN:SS";
  136. const year = date.getFullYear();
  137. const month =
  138. date.getMonth() + 1 < 10
  139. ? "0" + (date.getMonth() + 1)
  140. : date.getMonth() + 1;
  141. const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  142. const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  143. const minute =
  144. date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  145. const second =
  146. date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  147. format.indexOf("YYYY") > -1 ? (format = format.replace("YYYY", year)) : "";
  148. format.indexOf("MM") > -1 ? (format = format.replace("MM", month)) : "";
  149. format.indexOf("DD") > -1 ? (format = format.replace("DD", day)) : "";
  150. format.indexOf("HH") > -1 ? (format = format.replace("HH", hour)) : "";
  151. format.indexOf("NN") > -1 ? (format = format.replace("NN", minute)) : "";
  152. format.indexOf("SS") > -1 ? (format = format.replace("SS", second)) : "";
  153. return format;
  154. };
  155. //获取url参数组成json
  156. const GetQueryJson = e => {
  157. if (e == "undefined") {
  158. return {};
  159. } else {
  160. let url = e; //获取url
  161. let arr = []; //存储参数的数
  162. let res = {}; //存储最终JSON结果对象
  163. arr = url.split("?")[1].split("&"); //获取浏览器地址栏中的参数
  164. for (let i = 0; i < arr.length; i++) {
  165. //遍历参数
  166. if (arr[i].indexOf("=") != -1) {
  167. //如果参数中有值
  168. let str = arr[i].split("=");
  169. res[str[0]] = str[1];
  170. } else {
  171. //如果参数中无值
  172. res[arr[i]] = "";
  173. }
  174. }
  175. return res;
  176. }
  177. };
  178. module.exports = {
  179. isCardID: isCardID,
  180. isPhone: isPhone,
  181. isChineseWord: isChineseWord,
  182. formatTime: formatTime,
  183. GetQueryJson: GetQueryJson,
  184. isCornet
  185. };