check.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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) return false;
  62. if (idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(0, 2))]) {
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. },
  68. /*校验日期码*/
  69. checkBirthDayCode: function (birDayCode) {
  70. var check = /^[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))$/.test(birDayCode);
  71. if (!check) return false;
  72. var yyyy = parseInt(birDayCode.substring(0, 4), 10);
  73. var mm = parseInt(birDayCode.substring(4, 6), 10);
  74. var dd = parseInt(birDayCode.substring(6), 10);
  75. var xdata = new Date(yyyy, mm - 1, dd);
  76. if (xdata > new Date()) {
  77. return false; //生日不能大于当前日期
  78. } else if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd)) {
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. },
  84. /*计算校检码*/
  85. getParityBit: function (idCardNo) {
  86. var id17 = idCardNo.substring(0, 17);
  87. /*加权 */
  88. var power = 0;
  89. for (var i = 0; i < 17; i++) {
  90. power += parseInt(id17.charAt(i), 10) * parseInt(idCardNoUtil.powers[i]);
  91. }
  92. /*取模*/
  93. var mod = power % 11;
  94. return idCardNoUtil.parityBit[mod];
  95. },
  96. /*验证校检码*/
  97. checkParityBit: function (idCardNo) {
  98. var parityBit = idCardNo.charAt(17).toUpperCase();
  99. if (idCardNoUtil.getParityBit(idCardNo) == parityBit) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. },
  105. /*校验15位或18位的身份证号码*/
  106. checkIdCardNo: function (idCardNo) {
  107. //15位和18位身份证号码的基本校验
  108. var check = /^\d{15}|(\d{17}(\d|x|X))$/.test(idCardNo);
  109. if (!check) return false;
  110. //判断长度为15位或18位
  111. if (idCardNo.length == 15) {
  112. return idCardNoUtil.check15IdCardNo(idCardNo);
  113. } else if (idCardNo.length == 18) {
  114. return idCardNoUtil.check18IdCardNo(idCardNo);
  115. } else {
  116. return false;
  117. }
  118. },
  119. //校验15位的身份证号码
  120. check15IdCardNo: function (idCardNo) {
  121. //15位身份证号码的基本校验
  122. 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);
  123. if (!check) return false;
  124. //校验地址码
  125. var addressCode = idCardNo.substring(0, 6);
  126. check = idCardNoUtil.checkAddressCode(addressCode);
  127. if (!check) return false;
  128. var birDayCode = '19' + idCardNo.substring(6, 12);
  129. //校验日期码
  130. return idCardNoUtil.checkBirthDayCode(birDayCode);
  131. },
  132. //校验18位的身份证号码
  133. check18IdCardNo: function (idCardNo) {
  134. //18位身份证号码的基本格式校验
  135. 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);
  136. if (!check) return false;
  137. //校验地址码
  138. var addressCode = idCardNo.substring(0, 6);
  139. check = idCardNoUtil.checkAddressCode(addressCode);
  140. if (!check) return false;
  141. //校验日期码
  142. var birDayCode = idCardNo.substring(6, 14);
  143. check = idCardNoUtil.checkBirthDayCode(birDayCode);
  144. if (!check) return false;
  145. //验证校检码
  146. return idCardNoUtil.checkParityBit(idCardNo);
  147. },
  148. formateDateCN: function (day) {
  149. var yyyy = day.substring(0, 4);
  150. var mm = day.substring(4, 6);
  151. var dd = day.substring(6);
  152. return yyyy + '-' + mm + '-' + dd;
  153. },
  154. //获取信息
  155. getIdCardInfo: function (idCardNo) {
  156. var idCardInfo = {
  157. gender: "", //性别
  158. birthday: "" // 出生日期(yyyy-mm-dd)
  159. };
  160. if (idCardNo.length == 15) {
  161. var aday = '19' + idCardNo.substring(6, 12);
  162. idCardInfo.birthday = idCardNoUtil.formateDateCN(aday);
  163. if (parseInt(idCardNo.charAt(14)) % 2 == 0) {
  164. idCardInfo.gender = idCardNoUtil.genders.female;
  165. } else {
  166. idCardInfo.gender = idCardNoUtil.genders.male;
  167. }
  168. } else if (idCardNo.length == 18) {
  169. var aday = idCardNo.substring(6, 14);
  170. idCardInfo.birthday = idCardNoUtil.formateDateCN(aday);
  171. if (parseInt(idCardNo.charAt(16)) % 2 == 0) {
  172. idCardInfo.gender = idCardNoUtil.genders.female;
  173. } else {
  174. idCardInfo.gender = idCardNoUtil.genders.male;
  175. }
  176. }
  177. return idCardInfo;
  178. },
  179. /*18位转15位*/
  180. getId15: function (idCardNo) {
  181. if (idCardNo.length == 15) {
  182. return idCardNo;
  183. } else if (idCardNo.length == 18) {
  184. return idCardNo.substring(0, 6) + idCardNo.substring(8, 17);
  185. } else {
  186. return null;
  187. }
  188. },
  189. /*15位转18位*/
  190. getId18: function (idCardNo) {
  191. if (idCardNo.length == 15) {
  192. var id17 = idCardNo.substring(0, 6) + '19' + idCardNo.substring(6);
  193. var parityBit = idCardNoUtil.getParityBit(id17);
  194. return id17 + parityBit;
  195. } else if (idCardNo.length == 18) {
  196. return idCardNo;
  197. } else {
  198. return null;
  199. }
  200. }
  201. };
  202. function isCardCheck(nid) {
  203. return idCardNoUtil.check18IdCardNo(nid);
  204. }
  205. function getTimeList(type) {
  206. let timeList = [];
  207. switch (type) {
  208. case 1:
  209. timeList = [{
  210. id: 1,
  211. time: '08:30-09:30'
  212. },
  213. {
  214. id: 2,
  215. time: '10:45-11:45'
  216. },
  217. {
  218. id: 3,
  219. time: '13:00-14:00'
  220. },
  221. {
  222. id: 4,
  223. time: '14:45-15:45'
  224. },
  225. {
  226. id: 5,
  227. time: '17:30-18:30'
  228. },
  229. {
  230. id: 6,
  231. time: '20:00-21:00'
  232. },
  233. ];
  234. break;
  235. case 2:
  236. timeList = [{
  237. id: 1,
  238. time: '08:00-10:00'
  239. },
  240. {
  241. id: 2,
  242. time: '13:00-15:00'
  243. },
  244. {
  245. id: 3,
  246. time: '17:30-19:30'
  247. }
  248. ];
  249. break;
  250. case 3:
  251. timeList = [{
  252. id: 1,
  253. time: '08:00-11:00'
  254. },
  255. {
  256. id: 2,
  257. time: '13:00-16:00'
  258. },
  259. {
  260. id: 3,
  261. time: '17:30-20:30'
  262. }
  263. ];
  264. break;
  265. case 4:
  266. timeList = [{
  267. id: 1,
  268. time: '08:00-12:00'
  269. },
  270. {
  271. id: 2,
  272. time: '13:45-17:45'
  273. }
  274. ];
  275. break;
  276. case 6:
  277. timeList = [{
  278. id: 1,
  279. time: '08:00-14:00'
  280. },
  281. {
  282. id: 2,
  283. time: '13:45-19:45'
  284. }
  285. ];
  286. break;
  287. case 8:
  288. timeList = [{
  289. id: 1,
  290. time: '08:00-17:00'
  291. }];
  292. break;
  293. case 9:
  294. timeList = [{
  295. id: 1,
  296. time: '08:00-18:00'
  297. }];
  298. break;
  299. default:
  300. timeList = [];
  301. }
  302. return timeList;
  303. }
  304. module.exports = {
  305. isValidPhone,
  306. isCardCheck,
  307. getTimeList
  308. }