Authored by htoooth

fix password strength

@@ -2,66 +2,26 @@ @@ -2,66 +2,26 @@
2 * 计算密码复杂度 2 * 计算密码复杂度
3 */ 3 */
4 4
5 -function gettype(str, i) {  
6 - if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {  
7 - return 1;  
8 - } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {  
9 - return 2;  
10 - } else if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {  
11 - return 3;  
12 - }  
13 -  
14 - return 4;  
15 -}  
16 -  
17 -function isregular(cur, pre, type) {  
18 - var curCode = cur.charCodeAt(0);  
19 - var preCode = pre.charCodeAt(0);  
20 -  
21 - if (curCode - preCode === 0) {  
22 - return true;  
23 - }  
24 -  
25 - if (type !== 4 && (curCode - preCode === 1 || curCode - preCode === -1)) {  
26 - return true;  
27 - }  
28 -  
29 - return false;  
30 -}  
31 -  
32 -function getcomplex(curType, preType) {  
33 - if (preType === 0 || curType === preType) {  
34 - return 0;  
35 - } else if (curType === 4 || preType === 4) {  
36 - return 2; 5 +function computeComplex(password) {
  6 + var lengthS = 0;
  7 + var numS = 0;
  8 + var length = password.length;
  9 +
  10 + if (length < 6) {
  11 + lengthS = 0;
  12 + } else if (length > 6 && length < 16) {
  13 + lengthS = 1;
37 } else { 14 } else {
38 - return 1; 15 + lengthS = 2;
39 } 16 }
40 -}  
41 -  
42 -function computeComplex(password) {  
43 - var complex = 0,  
44 - length = password.length,  
45 - pre = '',  
46 - preType = 0,  
47 - i = 0,  
48 - cur,  
49 - curType;  
50 17
51 -  
52 - for (i = 0; i < length; i++) {  
53 - cur = password.charAt(i);  
54 - curType = gettype(password, i);  
55 -  
56 - if (preType !== curType || !isregular(cur, pre, curType)) {  
57 - complex += curType + getcomplex(curType, preType);  
58 - }  
59 -  
60 - pre = cur;  
61 - preType = curType; 18 + if (/^[0-9]+$/.test(password) || /^[A-Za-z]+$/.test(password) || length === 0) {
  19 + numS = 0;
  20 + } else {
  21 + numS = 1;
62 } 22 }
63 23
64 - return complex; 24 + return lengthS + numS;
65 } 25 }
66 26
67 module.exports = computeComplex; 27 module.exports = computeComplex;
@@ -332,19 +332,8 @@ function validatePassword() { @@ -332,19 +332,8 @@ function validatePassword() {
332 // 密码强度验证 332 // 密码强度验证
333 function validatePasswordComplexLocal($obj) { 333 function validatePasswordComplexLocal($obj) {
334 var pwd = $obj.val(), 334 var pwd = $obj.val(),
335 - pwdStrength = computeComplex(pwd),  
336 - level = 0;  
337 -  
338 - if (pwdStrength === 0) {  
339 - level = 0;  
340 - } else if (pwdStrength <= 10) {  
341 - level = 1;  
342 - } else if (pwdStrength <= 20) {  
343 - level = 2;  
344 - } else {  
345 - level = 3;  
346 - }  
347 - 335 + level = computeComplex(pwd);
  336 + console.log(level);
348 switch (level) { 337 switch (level) {
349 case 0: 338 case 0:
350 $pwdParent.removeClass('red yellow green'); 339 $pwdParent.removeClass('red yellow green');
@@ -483,7 +472,7 @@ exports.init = function() { @@ -483,7 +472,7 @@ exports.init = function() {
483 }); 472 });
484 473
485 // 验证密码输入 474 // 验证密码输入
486 - $passwordInput.on('keyup blur', function() { 475 + $passwordInput.on('keyup', function() {
487 var $this = $(this); 476 var $this = $(this);
488 477
489 validatePassword().always(function() { 478 validatePassword().always(function() {