1 (function($){
2 $.fn.shortPass = 'Too short';
3 $.fn.badPass = 'Weak';
4 $.fn.goodPass = 'Good';
5 $.fn.strongPass = 'Strong';
6 $.fn.samePassword = 'Username and Password identical.';
7 $.fn.resultStyle = "";
8
9 $.fn.passStrength = function(options) {
10
11 var defaults = {
12 shortPass: "shortPass", //optional
13 badPass: "badPass", //optional
14 goodPass: "goodPass", //optional
15 strongPass: "strongPass", //optional
16 baseStyle: "testresult", //optional
17 userid: "", //required override
18 messageloc: 1 //before == 0 or after == 1
19 };
20 var opts = $.extend(defaults, options);
21
22 return this.each(function() {
23 var obj = $(this);
24
25 $(obj).unbind().keyup(function()
26 {
27
28 var results = $.fn.teststrength($(this).val(),$(opts.userid).val(),opts);
29
30 if(opts.messageloc === 1)
31 {
32 $(this).next("." + opts.baseStyle).remove();
33 $(this).after("<span class=\""+opts.baseStyle+"\"><span></span></span>");
34 $(this).next("." + opts.baseStyle).addClass($(this).resultStyle).find("span").text(results);
35 }
36 else
37 {
38 $(this).prev("." + opts.baseStyle).remove();
39 $(this).before("<span class=\""+opts.baseStyle+"\"><span></span></span>");
40 $(this).prev("." + opts.baseStyle).addClass($(this).resultStyle).find("span").text(results);
41 }
42 });
43
44 //FUNCTIONS
45 $.fn.teststrength = function(password,username,option){
46 var score = 0;
47
48 //password < 4
49 if (password.length < 4 ) { this.resultStyle = option.shortPass;return $(this).shortPass; }
50
51 //password == user name
52 if (password.toLowerCase()==username.toLowerCase()){this.resultStyle = option.badPass;return $(this).samePassword;}
53
54 //password length
55 score += password.length * 4;
56 score += ( $.fn.checkRepetition(1,password).length - password.length ) * 1;
57 score += ( $.fn.checkRepetition(2,password).length - password.length ) * 1;
58 score += ( $.fn.checkRepetition(3,password).length - password.length ) * 1;
59 score += ( $.fn.checkRepetition(4,password).length - password.length ) * 1;
60
61 //password has 3 numbers
62 if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;}
63
64 //password has 2 symbols
65 if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;}
66
67 //password has Upper and Lower chars
68 if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;}
69
70 //password has number and chars
71 if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;}
72 //
73 //password has number and symbol
74 if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;}
75
76 //password has char and symbol
77 if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;}
78
79 //password is just a numbers or chars
80 if (password.match(/^\w+$/) || password.match(/^\d+$/) ){ score -= 10;}
81
82 //verifying 0 < score < 100
83 if ( score < 0 ){score = 0;}
84 if ( score > 100 ){ score = 100;}
85
86 if (score < 34 ){ this.resultStyle = option.badPass; return $(this).badPass;}
87 if (score < 68 ){ this.resultStyle = option.goodPass;return $(this).goodPass;}
88
89 this.resultStyle= option.strongPass;
90 return $(this).strongPass;
91
92 };
93
94 });
95 };
96 })(jQuery);
97
98
99 $.fn.checkRepetition = function(pLen,str) {
100 var res = "";
101 for (var i=0; i<str.length ; i++ )
102 {
103 var repeated=true;
104
105 for (var j=0;j < pLen && (j+i+pLen) < str.length;j++){
106 repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen));
107 }
108 if (j<pLen){repeated=false;}
109 if (repeated) {
110 i+=pLen-1;
111 repeated=false;
112 }
113 else {
114 res+=str.charAt(i);
115 }
116 }
117 return res;
118 };
119