-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuikit-strength.min.js
122 lines (108 loc) · 3.58 KB
/
uikit-strength.min.js
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
/*
* UIkit password strength
* based on bootstrap-strength.js
* Original author: @rivalex
* Further changes, comments: @rivalex
* Licensed under the MIT license
*/
;(function(window, $, undefined) {
"use strict";
$.UIkitStrength = function UIkitStrength(options,element) {
this.element = element;
this.$elem = $(this.element);
this.options = $.extend({}, $.UIkitStrength.defaults, options);
var _self = this;
_self._init();
};
$.UIkitStrength.defaults = {
stripped: true,
active: true,
slimBar: false,
minLenght: 8,
upperCase: 1,
upperReg: "[A-Z]",
lowerCase: 1,
lowerReg: "[a-z]",
numbers: 1,
numberReg: "[0-9]",
specialchars: 1,
specialReg: "[!,%,&,@,#,$,^,*,?,_,~]",
topMargin: "0;",
meterClasses: {
weak: "uk-label-danger",
medium: "uk-label-warning",
good: "uk-label-success"
}
};
$.UIkitStrength.prototype = {
_init: function() {
var _self = this,
options = _self.options,
progressClass = options.meterClasses,
element = _self.$elem,
elementID = element.attr('id'),
stringLenght = 0,
capitals = 0,
lowers = 0,
numbers = 0,
special = 0,
progress,
progressbar;
function calcPercentage(full, partial) {
return ((partial / full) * 100).toFixed(0);
}
function matchString(string, range, limit) {
var searchPattern, rx;
searchPattern = "(.*" + range + ".*){" + limit + ",}";
rx = new RegExp( searchPattern, "g" );
return (string.match(rx))?1:0;
}
function check_strength(string, elementID){
stringLenght = (string.length >= options.minLenght)?1:0;
capitals = (options.upperCase > 0) ? matchString(string, options.upperReg, options.upperCase) : 1;
lowers = (options.lowerCase > 0) ? matchString(string, options.lowerReg, options.lowerCase) : 1;
numbers = (options.numbers > 0) ? matchString(string, options.numberReg, options.numbers) : 1;
special = (options.specialchars > 0) ? matchString(string, options.specialReg, options.specialchars) : 1;
var totalpercent = calcPercentage(5, (stringLenght + capitals + lowers + numbers + special));
displayStrenght(totalpercent,elementID);
}
function displayStrenght(total, elementID){
var meter = $('div[data-meter="'+elementID+'"]'),
meterClass;
switch(parseInt(total)) {
case 0: meterClass = progressClass.weak; break;
case 20: meterClass = progressClass.weak; break;
case 40: meterClass = progressClass.weak; break;
case 60: meterClass = progressClass.medium; break;
case 80: meterClass = progressClass.medium; break;
case 100: meterClass = progressClass.good; break;
}
meter.removeClass(progressClass.weak + " " + progressClass.medium + " " + progressClass.good).attr("value",total).width(total + "%").addClass(meterClass);
}
progress = $("<div/>", {
class: "progress",
style: "margin-top:" + options.topMargin
});
progressbar = $("<div/>", {
class: "" + ((options.active)?" active":""),
// role: "progressbar",
"data-meter": elementID,
"value": 0,
// "min": 0,
"max": 100,
"style": ((options.slimBar) ? "height: 5px;" : "height: 10px;")
}).appendTo(progress);
this.$elem.parent().append(progress);
this.$elem.bind('keyup keydown', function(){
var thisString = $('#'+elementID).val();
check_strength(thisString,elementID);
});
}
};
$.fn.UIkitStrength = function (options) {
if($.data(this, "bootstrapStrength")){return;}
return $(this).each(function() {
$.data(this, "bootstrapStrength",new $.UIkitStrength(options, this));
});
};
})(window, jQuery);