Showing
3 changed files
with
98 additions
and
3 deletions
public/js/passport/password-check.js
0 → 100644
1 | +'use strict'; | ||
2 | + | ||
3 | +var config = { | ||
4 | + rules: [ | ||
5 | + {method: 'required', param: []}, | ||
6 | + {method: 'lte', param: [20]}, | ||
7 | + {method: 'gte', param: [6]}, | ||
8 | + {method: 'charLimit', param: []} | ||
9 | + ], | ||
10 | + msgs: { | ||
11 | + required: '由字母、数字组合,不能包含特殊字符', | ||
12 | + lte: '长度须在6-20个字符之间', | ||
13 | + gte: '长度须在6-20个字符之间', | ||
14 | + charLimit: '密码须为字母和数字组合' | ||
15 | + } | ||
16 | +}; | ||
17 | + | ||
18 | +var validators = { | ||
19 | + required: function(val) { | ||
20 | + return !!val.length; | ||
21 | + }, | ||
22 | + | ||
23 | + gte: function(val, param) { | ||
24 | + return val.length >= param; | ||
25 | + }, | ||
26 | + | ||
27 | + lte: function(val, param) { | ||
28 | + return val.length <= param; | ||
29 | + }, | ||
30 | + | ||
31 | + charLimit: function(val) { | ||
32 | + return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(val); | ||
33 | + } | ||
34 | +}; | ||
35 | + | ||
36 | +function validate(val, callback) { | ||
37 | + var rules = config.rules, | ||
38 | + msgs = config.msgs, | ||
39 | + i, ruleLen; | ||
40 | + | ||
41 | + var name, param, pass = true; | ||
42 | + | ||
43 | + // apple rule one by one; | ||
44 | + for (i = 0, ruleLen = rules.length; i < ruleLen; i++) { | ||
45 | + name = rules[i].method; | ||
46 | + param = rules[i].param.concat(); | ||
47 | + param.unshift(val); | ||
48 | + | ||
49 | + pass = validators[name].apply(this, param); | ||
50 | + | ||
51 | + if (!pass) { | ||
52 | + callback({ // eslint-disable-line | ||
53 | + valid: pass, | ||
54 | + msg: msgs[name] | ||
55 | + }); | ||
56 | + break; | ||
57 | + } | ||
58 | + } | ||
59 | + | ||
60 | + return pass; | ||
61 | +} | ||
62 | + | ||
63 | +// test | ||
64 | +/* eslint-disable */ | ||
65 | +// validate('1234567', function(result) {console.log(result)}) | ||
66 | +// validate('12345', function(result) {console.log(result)}); | ||
67 | +// validate('a'.repeat(21), function(result) {console.log(result)}); | ||
68 | +// validate('1abcdefg', function(result) {console.log(result)}); | ||
69 | +/* eslint-enable */ | ||
70 | + | ||
71 | + | ||
72 | +module.exports = validate; | ||
73 | + |
1 | 'use strict'; | 1 | 'use strict'; |
2 | 2 | ||
3 | -var tip, checkPoint; | 3 | +var tip, checkPoint, validatePWD; |
4 | var $eyeBtn, | 4 | var $eyeBtn, |
5 | $pwd, | 5 | $pwd, |
6 | - $nextBtn; | 6 | + $nextBtn, |
7 | + $pwdLint, | ||
8 | + $pwdLintTxt; | ||
7 | 9 | ||
8 | var page; | 10 | var page; |
9 | 11 | ||
12 | + | ||
10 | require('js/common'); | 13 | require('js/common'); |
11 | tip = require('plugin/tip'); | 14 | tip = require('plugin/tip'); |
12 | checkPoint = require('./smslogin/check-point'); | 15 | checkPoint = require('./smslogin/check-point'); |
16 | +validatePWD = require('./password-check'); | ||
13 | 17 | ||
14 | setTimeout(function() { | 18 | setTimeout(function() { |
15 | checkPoint('YB_SET_PASSWORD_L'); | 19 | checkPoint('YB_SET_PASSWORD_L'); |
@@ -24,6 +28,8 @@ page = { | @@ -24,6 +28,8 @@ page = { | ||
24 | $eyeBtn = $('#eye'); | 28 | $eyeBtn = $('#eye'); |
25 | $pwd = $('#pwd'); | 29 | $pwd = $('#pwd'); |
26 | $nextBtn = $('#btn-next'); | 30 | $nextBtn = $('#btn-next'); |
31 | + $pwdLint = $('.js-password').find('.pwd-lint'); | ||
32 | + $pwdLintTxt = $pwdLint.find('.pwd-lint-txt'); | ||
27 | }, | 33 | }, |
28 | bindEvent: function() { | 34 | bindEvent: function() { |
29 | var self = this; | 35 | var self = this; |
@@ -37,7 +43,14 @@ page = { | @@ -37,7 +43,14 @@ page = { | ||
37 | }); | 43 | }); |
38 | 44 | ||
39 | $pwd.on('input', function() { | 45 | $pwd.on('input', function() { |
40 | - var bool = Boolean($.trim(this.value)); | 46 | + var val = $.trim(this.value); |
47 | + var bool = validatePWD(val, function(res) { | ||
48 | + $pwdLint.css({visibility: res.valid}); | ||
49 | + | ||
50 | + if (!res.valid) { | ||
51 | + $pwdLintTxt.text(res.msg); | ||
52 | + } | ||
53 | + }); | ||
41 | 54 | ||
42 | $nextBtn | 55 | $nextBtn |
43 | .toggleClass('disable', !bool) | 56 | .toggleClass('disable', !bool) |
@@ -176,4 +176,13 @@ body.passport-body { | @@ -176,4 +176,13 @@ body.passport-body { | ||
176 | .row { | 176 | .row { |
177 | margin-bottom: 10PX; | 177 | margin-bottom: 10PX; |
178 | } | 178 | } |
179 | + | ||
180 | + .pwd-lint { | ||
181 | + text-align: left; | ||
182 | + color: #858585; | ||
183 | + | ||
184 | + .icon-tip { | ||
185 | + color: #4ecae8; | ||
186 | + } | ||
187 | + } | ||
179 | } | 188 | } |
-
Please register or login to post a comment