check.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. function isValidPhone(str) {
  2. var myreg = /^[1][0-9][0-9]{9}$/;
  3. //var myreg = /^1\d{10}$/;
  4. if (!myreg.test(str)) {
  5. return false;
  6. } else {
  7. return true;
  8. }
  9. }
  10. var idCardNoUtil = {
  11. /*省,直辖市代码表*/
  12. provinceAndCitys: {
  13. 11: '北京',
  14. 12: '天津',
  15. 13: '河北',
  16. 14: '山西',
  17. 15: '内蒙古',
  18. 21: '辽宁',
  19. 22: '吉林',
  20. 23: '黑龙江',
  21. 31: '上海',
  22. 32: '江苏',
  23. 33: '浙江',
  24. 34: '安徽',
  25. 35: '福建',
  26. 36: '江西',
  27. 37: '山东',
  28. 41: '河南',
  29. 42: '湖北',
  30. 43: '湖南',
  31. 44: '广东',
  32. 45: '广西',
  33. 46: '海南',
  34. 50: '重庆',
  35. 51: '四川',
  36. 52: '贵州',
  37. 53: '云南',
  38. 54: '西藏',
  39. 61: '陕西',
  40. 62: '甘肃',
  41. 63: '青海',
  42. 64: '宁夏',
  43. 65: '新疆',
  44. 71: '台湾',
  45. 81: '香港',
  46. 82: '澳门',
  47. 91: '国外'
  48. },
  49. /*每位加权因子*/
  50. powers: ['7', '9', '10', '5', '8', '4', '2', '1', '6', '3', '7', '9', '10', '5', '8', '4', '2'],
  51. /*第18位校检码*/
  52. parityBit: ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'],
  53. /*性别*/
  54. genders: {
  55. male: '男',
  56. female: '女'
  57. },
  58. /*校验地址码*/
  59. checkAddressCode: function (addressCode) {
  60. var check = /^[1-9]\d{5}$/.test(addressCode);
  61. if (!check) {
  62. return false;
  63. }
  64. if (idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(0, 2))]) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. },
  70. /*校验日期码*/
  71. checkBirthDayCode: function (birDayCode) {
  72. var check = /^[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))$/.test(birDayCode);
  73. if (!check) {
  74. return false;
  75. }
  76. var yyyy = parseInt(birDayCode.substring(0, 4), 10);
  77. var mm = parseInt(birDayCode.substring(4, 6), 10);
  78. var dd = parseInt(birDayCode.substring(6), 10);
  79. var xdata = new Date(yyyy, mm - 1, dd);
  80. if (xdata > new Date()) {
  81. return false; //生日不能大于当前日期
  82. } else if (xdata.getFullYear() == yyyy && xdata.getMonth() == mm - 1 && xdata.getDate() == dd) {
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. },
  88. /*计算校检码*/
  89. getParityBit: function (idCardNo) {
  90. var id17 = idCardNo.substring(0, 17);
  91. /*加权 */
  92. var power = 0;
  93. for (var i = 0; i < 17; i++) {
  94. power += parseInt(id17.charAt(i), 10) * parseInt(idCardNoUtil.powers[i]);
  95. }
  96. /*取模*/
  97. var mod = power % 11;
  98. return idCardNoUtil.parityBit[mod];
  99. },
  100. /*验证校检码*/
  101. checkParityBit: function (idCardNo) {
  102. var parityBit = idCardNo.charAt(17).toUpperCase();
  103. if (idCardNoUtil.getParityBit(idCardNo) == parityBit) {
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. },
  109. /*校验15位或18位的身份证号码*/
  110. checkIdCardNo: function (idCardNo) {
  111. //15位和18位身份证号码的基本校验
  112. var check = /^\d{15}|(\d{17}(\d|x|X))$/.test(idCardNo);
  113. if (!check) {
  114. return false;
  115. }
  116. //判断长度为15位或18位
  117. if (idCardNo.length == 15) {
  118. return idCardNoUtil.check15IdCardNo(idCardNo);
  119. } else if (idCardNo.length == 18) {
  120. return idCardNoUtil.check18IdCardNo(idCardNo);
  121. } else {
  122. return false;
  123. }
  124. },
  125. //校验15位的身份证号码
  126. check15IdCardNo: function (idCardNo) {
  127. //15位身份证号码的基本校验
  128. var check = /^[1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}$/.test(idCardNo);
  129. if (!check) {
  130. return false;
  131. }
  132. //校验地址码
  133. var addressCode = idCardNo.substring(0, 6);
  134. check = idCardNoUtil.checkAddressCode(addressCode);
  135. if (!check) {
  136. return false;
  137. }
  138. var birDayCode = '19' + idCardNo.substring(6, 12);
  139. //校验日期码
  140. return idCardNoUtil.checkBirthDayCode(birDayCode);
  141. },
  142. //校验18位的身份证号码
  143. check18IdCardNo: function (idCardNo) {
  144. //18位身份证号码的基本格式校验
  145. var check = /^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}(\d|x|X)$/.test(idCardNo);
  146. if (!check) {
  147. return false;
  148. }
  149. //校验地址码
  150. var addressCode = idCardNo.substring(0, 6);
  151. check = idCardNoUtil.checkAddressCode(addressCode);
  152. if (!check) {
  153. return false;
  154. }
  155. //校验日期码
  156. var birDayCode = idCardNo.substring(6, 14);
  157. check = idCardNoUtil.checkBirthDayCode(birDayCode);
  158. if (!check) {
  159. return false;
  160. }
  161. //验证校检码
  162. return idCardNoUtil.checkParityBit(idCardNo);
  163. },
  164. formateDateCN: function (day) {
  165. var yyyy = day.substring(0, 4);
  166. var mm = day.substring(4, 6);
  167. var dd = day.substring(6);
  168. return yyyy + '-' + mm + '-' + dd;
  169. },
  170. //获取信息
  171. getIdCardInfo: function (idCardNo) {
  172. var idCardInfo = {
  173. gender: '',
  174. //性别
  175. birthday: '' // 出生日期(yyyy-mm-dd)
  176. };
  177. if (idCardNo.length == 15) {
  178. var aday = '19' + idCardNo.substring(6, 12);
  179. idCardInfo.birthday = idCardNoUtil.formateDateCN(aday);
  180. if (parseInt(idCardNo.charAt(14)) % 2 == 0) {
  181. idCardInfo.gender = idCardNoUtil.genders.female;
  182. } else {
  183. idCardInfo.gender = idCardNoUtil.genders.male;
  184. }
  185. } else if (idCardNo.length == 18) {
  186. var aday = idCardNo.substring(6, 14);
  187. idCardInfo.birthday = idCardNoUtil.formateDateCN(aday);
  188. if (parseInt(idCardNo.charAt(16)) % 2 == 0) {
  189. idCardInfo.gender = idCardNoUtil.genders.female;
  190. } else {
  191. idCardInfo.gender = idCardNoUtil.genders.male;
  192. }
  193. }
  194. return idCardInfo;
  195. },
  196. /*18位转15位*/
  197. getId15: function (idCardNo) {
  198. if (idCardNo.length == 15) {
  199. return idCardNo;
  200. } else if (idCardNo.length == 18) {
  201. return idCardNo.substring(0, 6) + idCardNo.substring(8, 17);
  202. } else {
  203. return null;
  204. }
  205. },
  206. /*15位转18位*/
  207. getId18: function (idCardNo) {
  208. if (idCardNo.length == 15) {
  209. var id17 = idCardNo.substring(0, 6) + '19' + idCardNo.substring(6);
  210. var parityBit = idCardNoUtil.getParityBit(id17);
  211. return id17 + parityBit;
  212. } else if (idCardNo.length == 18) {
  213. return idCardNo;
  214. } else {
  215. return null;
  216. }
  217. }
  218. };
  219. function isCardCheck(nid) {
  220. return idCardNoUtil.check18IdCardNo(nid);
  221. }
  222. function getTimeList(type) {
  223. let timeList = [];
  224. switch (type) {
  225. case 1:
  226. timeList = [
  227. {
  228. id: 1,
  229. time: '08:30-09:30'
  230. },
  231. {
  232. id: 2,
  233. time: '10:45-11:45'
  234. },
  235. {
  236. id: 3,
  237. time: '13:00-14:00'
  238. },
  239. {
  240. id: 4,
  241. time: '14:45-15:45'
  242. },
  243. {
  244. id: 5,
  245. time: '17:30-18:30'
  246. },
  247. {
  248. id: 6,
  249. time: '20:00-21:00'
  250. }
  251. ];
  252. break;
  253. case 2:
  254. timeList = [
  255. {
  256. id: 1,
  257. time: '08:00-10:00'
  258. },
  259. {
  260. id: 2,
  261. time: '13:00-15:00'
  262. },
  263. {
  264. id: 3,
  265. time: '17:30-19:30'
  266. }
  267. ];
  268. break;
  269. case 3:
  270. timeList = [
  271. {
  272. id: 1,
  273. time: '08:00-11:00'
  274. },
  275. {
  276. id: 2,
  277. time: '13:00-16:00'
  278. },
  279. {
  280. id: 3,
  281. time: '17:30-20:30'
  282. }
  283. ];
  284. break;
  285. case 4:
  286. timeList = [
  287. {
  288. id: 1,
  289. time: '08:00-12:00'
  290. },
  291. {
  292. id: 2,
  293. time: '13:45-17:45'
  294. }
  295. ];
  296. break;
  297. case 6:
  298. timeList = [
  299. {
  300. id: 1,
  301. time: '08:00-14:00'
  302. },
  303. {
  304. id: 2,
  305. time: '13:45-19:45'
  306. }
  307. ];
  308. break;
  309. case 8:
  310. timeList = [
  311. {
  312. id: 1,
  313. time: '08:00-17:00'
  314. }
  315. ];
  316. break;
  317. case 9:
  318. timeList = [
  319. {
  320. id: 1,
  321. time: '08:00-18:00'
  322. }
  323. ];
  324. break;
  325. default:
  326. timeList = [];
  327. }
  328. return timeList;
  329. }
  330. module.exports = {
  331. isValidPhone,
  332. isCardCheck,
  333. getTimeList
  334. };