util.js
9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
function formatTime_date(date) {
var year = yearFormString(date)
var month = monthFormString(date)
var day = dayFormString(date)
return [year, month, day].map(formatNumber).join('-')
}
function formatTime_month(date) {
var year = yearFormString(date)
var month = monthFormString(date)
return [year, month].map(formatNumber).join('-')
}
function formatDate_min(date) {
var year = yearFormString(date)
var month = monthFormString(date)
var day = dayFormString(date)
return [year, month, day].map(formatNumber).join('/')
}
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
function formatDateString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var newDate = this.formatTime(date)
return newDate
}
function yearFormString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var year = date.getFullYear()
return year
}
function monthFormString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var month = date.getMonth() + 1
return month
}
function dayFormString(dateStr) {
var date = new Date()
date.setTime(dateStr * 1000)
var day = date.getDate()
return day
}
function getDateString(date_stamp){//时间戳转年月日
return yearFormString(date_stamp) + "年" + monthFormString(date_stamp) + "月" + dayFormString(date_stamp) + "日"
}
// 两个日期间隔天数
function daysTillNow(startDate, endDate){
var days = (endDate - startDate) / (24 * 60 * 60 );
return Math.ceil(days);
}
//获取当前时间,格式YYYY-MM-DD
function curDateTime() {
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
var date = d.getDate();
var curDateTime = year;
if (month > 9)
curDateTime = curDateTime + "-" + month;
else
curDateTime = curDateTime + "-0" + month;
if (date > 9)
curDateTime = curDateTime + "-" + date;
else
curDateTime = curDateTime + "-0" + date;
return curDateTime;
}
/**
* 检验18位身份证号码(15位号码可以只检测生日是否正确即可)
* @author wolfchen
* @param idCardNo 18为的身份证号码
* @return Boolean 是否合法
**/
function isIDCardNum(idCardNo) {
var regID_18 = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
var regID_15 = /^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$/;
var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//加权因子
var arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//校验码
if (regID_15.test(idCardNo)) {
return true;
} else if (regID_18.test(idCardNo)) {
var sum = 0, idx;
for (var i = 0; i < idCardNo.length - 1; i++) {
// 对前17位数字与权值乘积求和
sum += parseInt(idCardNo.substr(i, 1), 10) * arrExp[i];
}
// 计算模(固定算法)
idx = sum % 11;
// 检验第18为是否与校验码相等
return arrValid[idx] == idCardNo.substr(17, 1).toUpperCase();
} else {
return false;
}
}
function analyzeIDCard(IDCard){
var sexAndAge = {}
//获取用户身份证号码
var userCard = IDCard;
//如果身份证号码为undefind则返回空
if (!userCard) {
return sexAndAge;
}
//获取性别
if (parseInt(userCard.substr(16, 1)) % 2 == 1) {
sexAndAge.sex = '男'
} else {
sexAndAge.sex = '女'
}
//获取出生年月日
//userCard.substring(6,10) + "-" + userCard.substring(10,12) + "-" + userCard.substring(12,14);
var yearBirth = userCard.substring(6, 10);
var monthBirth = userCard.substring(10, 12);
var dayBirth = userCard.substring(12, 14);
//出生日期
sexAndAge.birthDay = yearBirth + '-' + monthBirth + '-' + dayBirth;
//获取当前年月日并计算年龄
var myDate = new Date();
var monthNow = myDate.getMonth() + 1;
var dayNow = myDate.getDate();
var age = myDate.getFullYear() - yearBirth;
if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
age--;
}
//得到年龄
sexAndAge.age = age;
//返回性别和年龄
return sexAndAge;
}
function isMobile(mobile) {
var regMobile = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
return regMobile.test(mobile)
}
//验证字符串是否是数字
function checkNumber(theObj) {
var reg01 = /^[0-9]+.?[0-9]*$/;
var reg02 = /^[0-9]+$/;
if (reg01.test(theObj) || reg02.test(theObj)) {
return true;
}
return false;
}
function getAge(birthdate) {
var yearBirth = birthdate.substring(0, 4);
var monthBirth = birthdate.substring(5, 7);
var dayBirth = birthdate.substring(8, 10);
//获取当前年月日并计算年龄
var myDate = new Date();
var monthNow = myDate.getMonth() + 1;
var dayNow = myDate.getDate();
var age = myDate.getFullYear() - yearBirth;
if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
age--;
}
return age;
}
//银行卡号码检测
function banknoCheck(bankno) {
var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位(与luhn进行比较)
var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
var newArr = new Array();
for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
newArr.push(first15Num.substr(i, 1));
}
var arrJiShu = new Array(); //奇数位*2的积 <9
var arrJiShu2 = new Array(); //奇数位*2的积 >9
var arrOuShu = new Array(); //偶数位数组
for (var j = 0; j < newArr.length; j++) {
if ((j + 1) % 2 == 1) { //奇数位
if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
else arrJiShu2.push(parseInt(newArr[j]) * 2);
} else //偶数位
arrOuShu.push(newArr[j]);
}
var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
for (var h = 0; h < arrJiShu2.length; h++) {
jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
}
var sumJiShu = 0; //奇数位*2 < 9 的数组之和
var sumOuShu = 0; //偶数位数组之和
var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
var sumTotal = 0;
for (var m = 0; m < arrJiShu.length; m++) {
sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
}
for (var n = 0; n < arrOuShu.length; n++) {
sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
}
for (var p = 0; p < jishu_child1.length; p++) {
sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
}
//计算总和
sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
//计算luhn值
var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
var luhn = 10 - k;
if (lastNum == luhn) {
// $("#banknoInfo").html("luhn验证通过");
return true;
} else {
// $("#banknoInfo").html("银行卡号必须符合luhn校验");
return false;
}
}
function checkEmail(email){
// var regEmail = /^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\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{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$/;
// var regEmail = new RegExp("^[-a-z0-9~!$%^&;*_=+}{/'?]+(/.[-a-z0-9~!$ %^&;*_=+}{/'?]+)*@([a-z0-9_][-a-z0-9_]*(/.[-a-z0-9_]+)* /.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}))(:[0-9]{1,5})?$");
// return regEmail.test(email);
}
module.exports = {
formatTime: formatTime,
formatTime_date: formatTime_date,
formatTime_month: formatTime_month,
formatDate_min: formatDate_min,
formatDateString: formatDateString,
yearFormString: yearFormString,
monthFormString: monthFormString,
dayFormString: dayFormString,
isIDCardNum: isIDCardNum,
curDateTime: curDateTime,
analyzeIDCard: analyzeIDCard,
checkNumber: checkNumber,
isMobile: isMobile,
getAge: getAge,
banknoCheck: banknoCheck,
checkEmail: checkEmail,
daysTillNow: daysTillNow,
getDateString: getDateString
}