api.js
3.84 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
/**
* 登录注册公用API
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/10/8
*/
var $ = require('yoho.jquery');
var trim = $.trim;
//邮箱验证规则
var emailRegx = /^([a-zA-Z0-9]+[_|\_|\.|-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.|-]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
//手机号码验证规则
var phoneRegx = {
'+86': /^1[35847]{1}[0-9]{9}$/,
'+852': /^[965]{1}[0-9]{7}$/,
'+853': /^[0-9]{8}$/,
'+886': /^[0-9]{10}$/,
'+65': /^[98]{1}[0-9]{7}$/,
'+60': /^1[1234679]{1}[0-9]{8}$/,
'+1': /^[0-9]{10}$/,
'+82': /^01[0-9]{9}$/,
'+44': /^7[789][0-9]{8}$/,
'+81': /^0[9|8|7][0-9]{9}$/,
'+61': /^[0-9]{11}$/
};
//错误验证
var $errTip,
tipTime;
/**
* 初始化错误提示
*/
function initErrTip() {
var errTipHtml = '<div id="err-tip" class="err-tip"></div>';
//插入错误提示HTML
$('.passport-page').append(errTipHtml);
$errTip = $('#err-tip');
$errTip.on('touchstart', function() {
$errTip.fadeOut();
//清除Timeout
clearTimeout(tipTime);
});
}
/**
* 显示错误提示
*/
function showErrTip(content) {
if (typeof $errTip === 'undefined') {
return;
}
$errTip.text(content).show();
//若2秒内未点击则自动消失
tipTime = setTimeout(function() {
if ($errTip.css('display') === 'block') {
$errTip.fadeOut();
}
}, 2000);
}
//密码显示隐藏
function bindEyesEvt() {
var $hasEye = $('.has-eye'),
$eye;
$hasEye.append('<div class="eye close"></div>');
$eye = $hasEye.children('.eye');
$eye.on('touchstart', function(e) {
var $this = $(this),
$pwd = $this.siblings('.pwd');
e.preventDefault();
$this.toggleClass('close');
//切换密码显示和文本显示
if ($this.hasClass('close')) {
$pwd.attr('type', 'password');
} else {
$pwd.attr('type', 'text');
}
$pwd.focus();
});
}
// 清空账号显示
function bindClearEvt() {
var $hasClear = $('.has-clear'),
$clear;
$hasClear.append('<div class="clear-input"></div>');
$clear = $hasClear.children('.clear-input');
$clear.on('touchstart', function(e) {
var $input = $clear.siblings('.input');
$input.val('').trigger('input').focus();
e.preventDefault();
});
//反向逻辑
$hasClear.children('.input').bind('input', function() {
var $this = $(this),
$thisClear = $this.siblings('.clear-input'),
val = trim($this.val());
if (val === '') {
$thisClear.hide();
} else {
$thisClear.show();
}
});
}
// 密码长度验证
function pwdValidate(pwd) {
if (pwd.length >= 6 && pwd.length <= 20) {
return true;
}
return false;
}
// hack for resolving direction:rtl didn't work in android uc
function selectCssHack($countrySelect) {
var u = navigator.userAgent;
function autoSelectWidth() {
var wordCount = $countrySelect.find('option:selected').text().length;
switch (wordCount) {
//分别有2,3,4个汉字的情况
case 2:
$countrySelect.outerWidth(90);
break;
case 3:
$countrySelect.outerWidth(110);
break;
default:
$countrySelect.outerWidth(130);
}
}
if (u.match(/uc/i) && u.match(/android/i)) {
$countrySelect.change(function() {
autoSelectWidth();
});
} else {
$countrySelect.removeClass('in-android-uc');
}
}
//Exports APIs
module.exports = {
emailRegx: emailRegx,
phoneRegx: phoneRegx,
initErrTip: initErrTip,
showErrTip: showErrTip,
bindEyesEvt: bindEyesEvt,
bindClearEvt: bindClearEvt,
pwdValidate: pwdValidate,
selectCssHack: selectCssHack
};