check.js 8.4 KB

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