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'])
84 initEvents: function () {
85 Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
87 if (this.el.is('input[type=password]') && Roo.isSafari) {
88 this.el.on('keydown', this.SafariOnKeyDown, this);
91 this.el.on('keyup', this.checkStrength, this, {buffer: 50});
94 onRender: function (ct, position) {
95 Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
96 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
97 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
99 this.trigger.createChild({
101 cls: 'roo-password-meter-container col-xs-12',
103 //width: this.meterWidth + 'px'
107 cls: 'roo-password-meter-grey',
109 //width: this.meterWidth + 'px'
115 cls: 'roo-password-meter-grey col-xs-12',
118 //width: this.meterWidth + 'px'
122 cls: 'roo-password-meter-text'
127 if (this.hideTrigger) {
128 this.trigger.setDisplayed(false);
130 this.setSize(this.width || '', this.height || '');
133 onDestroy: function () {
135 this.trigger.removeAllListeners();
136 this.trigger.remove();
141 Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
144 checkStrength: function () {
145 var pwd = this.inputEl().getValue();
146 if (pwd == this._lastPwd) {
151 if (this.ClientSideStrongPassword(pwd)) {
153 } else if (this.ClientSideMediumPassword(pwd)) {
155 } else if (this.ClientSideWeakPassword(pwd)) {
161 console.log('strength1: ' + strength);
163 //var pm = this.trigger.child('div/div/div').dom;
164 var pm = this.trigger.child('div/div/div');
165 pm.removeClass(this.meterClass);
166 pm.addClass(this.meterClass[strength]);
169 var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;
171 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
176 Roo.bootstrap.SecurePass.superclass.reset.call(this);
180 var pm = this.trigger.child('div/div/div');
181 pm.removeClass(this.meterClass);
182 pm.addClass('roo-password-meter-grey');
185 var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;
188 this.inputEl().dom.type='password';
191 validateValue: function (value) {
193 if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
196 if (value.length == 0) {
197 if (this.allowBlank) {
202 this.markInvalid(this.errors.PwdEmpty);
203 this.errorMsg = this.errors.PwdEmpty;
211 if ('[\x21-\x7e]*'.match(value)) {
212 this.markInvalid(this.errors.PwdBadChar);
213 this.errorMsg = this.errors.PwdBadChar;
216 if (value.length < 6) {
217 this.markInvalid(this.errors.PwdShort);
218 this.errorMsg = this.errors.PwdShort;
221 if (value.length > 16) {
222 this.markInvalid(this.errors.PwdLong);
223 this.errorMsg = this.errors.PwdLong;
227 if (this.ClientSideStrongPassword(value)) {
229 } else if (this.ClientSideMediumPassword(value)) {
231 } else if (this.ClientSideWeakPassword(value)) {
239 //this.markInvalid(this.errors.TooWeak);
240 this.errorMsg = this.errors.TooWeak;
245 console.log('strength2: ' + strength);
247 //var pm = this.trigger.child('div/div/div').dom;
249 var pm = this.trigger.child('div/div/div');
250 pm.removeClass(this.meterClass);
251 pm.addClass(this.meterClass[strength]);
253 var pt = this.trigger.child('/div/div/').child('>*[class=roo-password-meter-text]').dom;
255 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
261 CharacterSetChecks: function (type) {
263 this.fResult = false;
266 isctype: function (character, type) {
267 switch (type) { //why needed break after return in js ? very odd bug
268 case this.kCapitalLetter:
269 if (character >= 'A' && character <= 'Z') {
273 case this.kSmallLetter:
274 if (character >= 'a' && character <= 'z') {
279 if (character >= '0' && character <= '9') {
283 case this.kPunctuation:
284 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
294 IsLongEnough: function (pwd, size) {
295 return !(pwd == null || isNaN(size) || pwd.length < size);
298 SpansEnoughCharacterSets: function (word, nb) {
299 if (!this.IsLongEnough(word, nb))
304 var characterSetChecks = new Array(
305 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
306 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
307 for (var index = 0; index < word.length; ++index) {
308 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
309 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
310 characterSetChecks[nCharSet].fResult = true;
317 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
318 if (characterSetChecks[nCharSet].fResult) {
323 if (nCharSets < nb) {
329 ClientSideStrongPassword: function (pwd) {
330 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
333 ClientSideMediumPassword: function (pwd) {
334 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
337 ClientSideWeakPassword: function (pwd) {
338 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);