validate.js
7.45 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
/**
* 个人中心页-账号安全验证
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/02/23
*/
var $ = require('yoho.jquery');
var $checkUser = $('.check-user'),
$checkInput = $checkUser.find('input').not('input[type=button],input[type=hidden]'),
canSend = true,
stime = 60,
sInt;
// 身份校验
function checkForm(dom) {
var val = dom.val(),
len = val.length,
$domParent = dom.parent(),
$checkInfo = $domParent.find('.check-info'),
inputName = dom.attr('name'),
regular = '';
$checkInfo.html('');
if (inputName === 'oldpassword') {
if (len === 0) {
$checkInfo.html('<div class="form-error">密码不能为空!</div>');
dom.addClass('input-error');
return false;
} else {
$.get('/home/account/pass?pass=' + encodeURIComponent(val), function(data) {
if (typeof data.code !== 'undefined' && data.code === 200) {
$checkInfo.html('<div class="form-success"> </div>');
dom.removeClass('input-error');
return true;
} else {
$checkInfo.html('<div class="form-error">密码错误!</div>');
dom.addClass('input-error');
return false;
}
});
}
}
if (inputName === 'code') {
$.get('/home/account/review?code=' + val, function(data) {
if (typeof data.code !== 'undefined' && data.code === 200) {
$checkInfo.html('<div class="form-success"> </div>');
dom.removeClass('input-error');
return true;
} else {
$checkInfo.html('<div class="form-error">验证码错误!</div>');
dom.addClass('input-error');
return false;
}
});
}
if (inputName === 'newmail') {
regular = '([a-zA-Z0-9]+)@([a-zA-Z0-9]+)[\.]([a-zA-Z0-9]+)';
if (val.match(regular) === null) {
$checkInfo.html('<div class="form-error">邮箱错误!</div>');
dom.addClass('input-error');
return false;
} else {
$.get('/home/account/mail?mail=' + val, function(data) {
if (typeof data.code !== 'undefined' && data.code === 200) {
$checkInfo.html('<div class="form-success"> </div>');
dom.removeClass('input-error');
return true;
} else {
$checkInfo.html('<div class="form-error">邮箱已存在!</div>');
dom.addClass('input-error');
return false;
}
});
}
}
if (inputName === 'mobile') {
regular = '^1[35847]{1}[0-9]{9}';
if (len === 0) {
$checkInfo.html('<div class="form-error">手机号不能为空!</div>');
dom.addClass('input-error');
return false;
} else if (val.match(regular) === null) {
$checkInfo.html('<div class="form-error">手机号错误!</div>');
dom.addClass('input-error');
return false;
} else {
$.get('/home/mobile/exmobi?mobile=' + val, function(data) {
if (typeof data.code !== 'undefined' && data.code !== 200) {
dom.addClass('input-error');
$checkInfo.html('<div class="form-error">手机号已经存在!</div>');
return false;
} else {
dom.removeClass('input-error');
$checkInfo.html('<div class="form-success"> </div>');
return true;
}
});
}
}
if (inputName === 'password') {
if (len < 6 || len > 20) {
dom.addClass('input-error');
$checkInfo.html('<div class="form-error">密码长度为6-20字符</div>');
return false;
} else {
dom.removeClass('input-error');
$checkInfo.html('<div class="form-success"> </div>');
return true;
}
}
if (inputName === 'confirm_password') {
if ($('#password').val() !== val) {
dom.addClass('input-error');
$checkInfo.html('<div class="form-error">两次密码不一致!</div>');
return false;
} else if ($('#password').val() !== '') {
dom.removeClass('input-error');
$('#password').next().html('<div class="form-success"> </div>');
$checkInfo.html('<div class="form-success"> </div>');
return true;
}
}
return true;
}
// 校验表单
function checkAllForm() {
var arr = [];
$.each($checkInput, function(key, item) {
arr.push(checkForm($(item)));
});
if (arr.indexOf(false) >= 0) {
return false;
} else {
return true;
}
}
// 切换验证码
function changeCode() {
var timestamp = (new Date()).getTime();
$('#the-code-img').attr('src', '/home/account/code?g=email_auth&time=' + timestamp);
}
// 重新发送倒计时
function code() {
var sstring = '';
if (stime > 0) {
sstring = '重新发送' + stime + '秒';
$('#sendButton').text(sstring);
stime = stime - 1;
} else {
stime = 60;
$('#sendButton').text('发送验证码');
clearInterval(sInt);
canSend = true;
}
}
// 发送手机验证码
function sendcode() {
var $mobile = $('#mobilevalue'),
$code = $('#inputcode'),
$mcheckInfo = $mobile.next(),
mobileV = $mobile.val(),
$ccheckInfo = $code.parent().find('check-info');
if (canSend) {
if (mobileV.match('^1[35847]{1}[0-9]{9}') === null) {
$mcheckInfo.html('<div class="form-error">手机号错误!</div>');
$mobile.addClass('input-error');
return false;
}
$.get('/home/mobile/sendcode?mobile=' + mobileV, function(data) {
if (typeof data.code !== 'undefined' && data.code === 200) {
canSend = false;
sInt = setInterval(function() {
code();
}, 1000);
$ccheckInfo.html('');
} else {
$ccheckInfo.html('<div class="form-error">验证码发送失败</div>');
}
});
} else {
return false;
}
}
//验证完成后倒计时跳转
function toHome() {
window.location.href = '/home/account';
}
$checkInput.blur(function() {
checkForm($(this));
});
$('.sub-btn').on('click', function() {
if (checkAllForm()) {
$('#pwdform').submit();
} else {
return false;
}
});
$('.the-code').on('click', function() {
changeCode();
});
$('#send-mobile-code').on('click', function() {
sendcode();
});
$('#inputcode').change(function() {
var code = $('#inputcode').val();
if (code !== '') {
$.get('/home/mobile/review?code=' + code, function(data) {
if (typeof data.code !== 'undefined' && data.code === 200) {
$('#codetip').html('<div class="form-success"> </div>');
} else {
$('#codetip').html('<div class="form-error">验证码错误!</div>');
}
});
}
});
$(function() {
var t = null;
if ($('.res-info').length > 0) {
t = setTimeout(function() {
toHome();
}, 5000);
}
changeCode();
});