smsVerify.js
3.54 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
(function($) {
// 插件的定义
$.fn.smsVerify = function(options) {
var defaults = {
'sendSmsButton' : '#sendSmsButton',
'sendSmsUrl' : '/center/smscode',
'validatorUrl' : '/center/validator',
'validatorButton' : '',
'mobileText' : '#mobile_num',
'errorBox' : '#checkInfoBox',
'maxTime' : 60,
'btnClass' : 'btn_b_gr',
'retryBtnClass' : 'btn_retry',
'sendType' : 'validator',
'mobileCodeTxt' : '#mobile_code',
'codeLength' : 4,
'infoBox' : '#checkMobileBox',
'infoTpl' : '#verifyMobileSuccess',
'callback' : ''
};
var opts = $.extend(defaults, options);
if(typeof opts.uid == undefined || opts.uid == 0)
{
window.console.log('用户ID错误');
return;
}
var mobile = $(opts.mobileText).val();
var time = opts.maxTime || 60;
// iterate and reformat each matched element
return this.each(function() {
$this = $(this);
$this.click(function(){
methods.init(opts).sendSms();
});
if(opts.validatorButton != ''){
$(opts.validatorButton).click(function(){
methods.init(opts).sendValidator();
});
}
});
};
var methods = {
options : {},
mobile : '',
time : 60,
init :function(options){
this.options = options;
this.mobile = $(options.mobileText).val();
this.time = options.maxTime;
return this;
},
sendSms : function(){
$(this.options.sendSmsButton).attr('disabled', true);
if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.mobile)))
{
alert('手机号码错误');
$(this.options.sendSmsButton).attr('disabled', false);
return;
}
$.getJSON('/center/smscode',{'mobile':this.mobile,'sendType':this.options.sendType},function(jsonData){
if(jsonData.code != 200)
{
$(methods.options.sendSmsButton).attr('disabled', false);
return;
}
$(methods.options.sendSmsButton).removeClass(methods.options.btnClass).addClass(methods.options.retryBtnClass).html('(<span id="timeBox">'+methods.time+'秒</span>)后重新发送');
methods.changeTime();
});
},
changeTime : function()
{
methods.time--;
if(methods.time > 0){
$('#timeBox').html(methods.time + '秒');
setTimeout(methods.changeTime,1000);
}else{
$(methods.options.sendSmsButton).removeClass(methods.options.retryBtnClass).addClass(methods.options.btnClass).html('重新发送');
$(methods.options.sendSmsButton).attr('disabled', false);
}
},
sendValidator : function(){
$(methods.options.validatorButton).attr('disabled', true);
var code = $(this.options.mobileCodeTxt).val();
if(!/^[0-9]{4}$/.test(code)){
$(this.options.errorBox).html('校验码错误,请输入正确的校验码!').show();
$(this.options.validatorButton).attr('disabled', false);
return;
}
$.getJSON('/center/validator', {'mobile':this.mobile,'mobile_code' : code, 'sendType' : this.options.sendType}, function (jsonData){
if(jsonData.code != 200){
$(methods.options.errorBox).html('手机校验码错误!').show();
$(methods.options.validatorButton).attr('disabled', false);
return;
}
$(methods.options.infoBox).html($(methods.options.infoTpl).html()).removeClass('cod_red').addClass('cod_green');
$(methods.options.validatorButton).attr('disabled', false);
if(typeof methods.options.callback != undefined && methods.options.callback != '')
{
eval(methods.options.callback +'()',1);
}
return;
});
}
};
// 闭包结束
})(jQuery);