9 * @class Roo.bootstrap.SecurePass
10 * @extends Roo.bootstrap.Input
11 * Bootstrap SecurePass class
15 * Create a new SecurePass
16 * @param {Object} config The config object
19 Roo.bootstrap.SecurePass = function (config) {
20 // these go here, so the translation tool can replace them..
22 PwdEmpty: "Please type a password, and then retype it to confirm.",
23 PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
24 PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
25 PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
26 IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
27 FNInPwd: "Your password can't contain your first name. Please type a different password.",
28 LNInPwd: "Your password can't contain your last name. Please type a different password.",
29 TooWeak: "Your password is Too Weak."
31 this.meterLabel = "Password strength:";
32 this.pwdStrengths = ["Too Weak", "Weak", "Medium", "Strong"];
33 this.meterClass = ["roo-password-meter-tooweak",
34 "roo-password-meter-weak",
35 "roo-password-meter-medium",
36 "roo-password-meter-strong",
37 "roo-password-meter-grey"],
38 Roo.bootstrap.SecurePass.superclass.constructor.call(this, config);
41 Roo.extend(Roo.bootstrap.SecurePass, Roo.bootstrap.Input, {
43 * @cfg {String/Object} errors A Error spec, or true for a default spec (defaults to
45 * PwdEmpty: "Please type a password, and then retype it to confirm.",
46 * PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
47 * PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
48 * PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
49 * IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
50 * FNInPwd: "Your password can't contain your first name. Please type a different password.",
51 * LNInPwd: "Your password can't contain your last name. Please type a different password."
61 * @cfg {String/Object} Label for the strength meter (defaults to
62 * 'Password strength:')
67 * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
68 * ['Weak', 'Medium', 'Strong'])
85 initEvents: function () {
86 Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
88 if (this.el.is('input[type=password]') && Roo.isSafari) {
89 this.el.on('keydown', this.SafariOnKeyDown, this);
92 this.el.on('keyup', this.checkStrength, this, {buffer: 50});
95 onRender: function (ct, position) {
96 Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
97 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
98 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
100 this.trigger.createChild({
102 cls: 'roo-password-meter-container col-xs-12',
104 //width: this.meterWidth + 'px'
108 cls: 'roo-password-meter-grey',
110 //width: this.meterWidth + 'px'
116 cls: 'roo-password-meter-grey col-xs-12',
119 //width: this.meterWidth + 'px'
123 cls: 'roo-password-meter-text'
128 if (this.hideTrigger) {
129 this.trigger.setDisplayed(false);
131 this.setSize(this.width || '', this.height || '');
134 onDestroy: function () {
136 this.trigger.removeAllListeners();
137 this.trigger.remove();
142 Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
145 checkStrength: function () {
146 var pwd = this.inputEl().getValue();
147 if (pwd == this._lastPwd) {
152 if (this.ClientSideStrongPassword(pwd)) {
154 } else if (this.ClientSideMediumPassword(pwd)) {
156 } else if (this.ClientSideWeakPassword(pwd)) {
162 console.log('strength1: ' + strength);
164 //var pm = this.trigger.child('div/div/div').dom;
165 var pm = this.trigger.child('div/div/div');
166 pm.removeClass(this.meterClass);
167 pm.addClass(this.meterClass[strength]);
169 //pm.style.width = (this.meterWidth / 3) * strength + 'px';
172 var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;
174 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
179 Roo.bootstrap.SecurePass.superclass.reset.call(this);
182 //var pm = this.trigger.child('div/div/div').dom;
183 //pm.style.width = 0;
184 var pm = this.trigger.child('div/div/div');
185 pm.removeClass(this.meterClass);
186 pm.addClass('roo-password-meter-grey');
189 var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;
191 //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
194 this.inputEl().dom.type='password';
197 validateValue: function (value) {
199 if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
202 if (value.length == 0) {
203 if (this.allowBlank) {
208 this.markInvalid(this.errors.PwdEmpty);
209 this.errorMsg = this.errors.PwdEmpty;
217 if ('[\x21-\x7e]*'.match(value)) {
218 this.markInvalid(this.errors.PwdBadChar);
219 this.errorMsg = this.errors.PwdBadChar;
222 if (value.length < 6) {
223 this.markInvalid(this.errors.PwdShort);
224 this.errorMsg = this.errors.PwdShort;
227 if (value.length > 16) {
228 this.markInvalid(this.errors.PwdLong);
229 this.errorMsg = this.errors.PwdLong;
233 if (this.ClientSideStrongPassword(value)) {
235 } else if (this.ClientSideMediumPassword(value)) {
237 } else if (this.ClientSideWeakPassword(value)) {
245 //this.markInvalid(this.errors.TooWeak);
246 this.errorMsg = this.errors.TooWeak;
251 console.log('strength2: ' + strength);
253 //var pm = this.trigger.child('div/div/div').dom;
255 var pm = this.trigger.child('div/div/div');
256 pm.removeClass(this.meterClass);
257 pm.addClass(this.meterClass[strength]);
259 var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;
261 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
267 CharacterSetChecks: function (type) {
269 this.fResult = false;
272 isctype: function (character, type) {
273 switch (type) { //why needed break after return in js ? very odd bug
274 case this.kCapitalLetter:
275 if (character >= 'A' && character <= 'Z') {
279 case this.kSmallLetter:
280 if (character >= 'a' && character <= 'z') {
285 if (character >= '0' && character <= '9') {
289 case this.kPunctuation:
290 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
300 IsLongEnough: function (pwd, size) {
301 return !(pwd == null || isNaN(size) || pwd.length < size);
304 SpansEnoughCharacterSets: function (word, nb) {
305 if (!this.IsLongEnough(word, nb))
310 var characterSetChecks = new Array(
311 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
312 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
313 for (var index = 0; index < word.length; ++index) {
314 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
315 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
316 characterSetChecks[nCharSet].fResult = true;
323 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
324 if (characterSetChecks[nCharSet].fResult) {
329 if (nCharSets < nb) {
335 ClientSideStrongPassword: function (pwd) {
336 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
339 ClientSideMediumPassword: function (pwd) {
340 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
343 ClientSideWeakPassword: function (pwd) {
344 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);