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 Roo.bootstrap.SecurePass.superclass.constructor.call(this, config);
36 Roo.extend(Roo.bootstrap.SecurePass, Roo.bootstrap.Input, {
38 * @cfg {String/Object} errors A Error spec, or true for a default spec (defaults to
40 * PwdEmpty: "Please type a password, and then retype it to confirm.",
41 * PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
42 * PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
43 * PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
44 * IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
45 * FNInPwd: "Your password can't contain your first name. Please type a different password.",
46 * LNInPwd: "Your password can't contain your last name. Please type a different password."
50 meterClass : ["password-meter-tooweak", "password-meter-weak", "password-meter-medium", "password-meter-strong"],
56 * @cfg {String/Object} Label for the strength meter (defaults to
57 * 'Password strength:')
62 * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
63 * ['Weak', 'Medium', 'Strong'])
79 initEvents: function () {
80 Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
82 if (this.el.is('input[type=password]') && Roo.isSafari) {
83 this.el.on('keydown', this.SafariOnKeyDown, this);
86 this.el.on('keyup', this.checkStrength, this, {buffer: 50});
89 onRender: function (ct, position) {
90 Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
91 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
92 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
94 this.trigger.createChild({
96 cls: 'password-meter-container col-xs-18',
98 //width: this.meterWidth + 'px'
102 cls: 'password-meter-grey',
104 width: this.meterWidth + 'px'
110 cls: 'password-meter-tooweek col-xs-12',
113 //width: this.meterWidth + 'px'
117 cls: 'password-meter-text'
122 if (this.hideTrigger) {
123 this.trigger.setDisplayed(false);
125 this.setSize(this.width || '', this.height || '');
128 onDestroy: function () {
130 this.trigger.removeAllListeners();
131 this.trigger.remove();
136 Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
139 checkStrength: function () {
140 var pwd = this.inputEl().getValue();
141 if (pwd == this._lastPwd) {
146 if (this.ClientSideStrongPassword(pwd)) {
148 } else if (this.ClientSideMediumPassword(pwd)) {
150 } else if (this.ClientSideWeakPassword(pwd)) {
156 console.log('strength1: ' + strength);
158 //var pm = this.trigger.child('div/div/div').dom;
159 var pm = this.trigger.child('div/div/div');
160 pm.removeClass(this.meterClass);
161 pm.addClass(this.meterClass[strength]);
163 //pm.style.width = (this.meterWidth / 3) * strength + 'px';
166 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
168 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
173 Roo.bootstrap.SecurePass.superclass.reset.call(this);
176 //var pm = this.trigger.child('div/div/div').dom;
177 //pm.style.width = 0;
178 var pm = this.trigger.child('div/div/div');
179 pm.removeClass(this.meterClass);
180 pm.addClass(this.meterClass[0]);
183 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
185 //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
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=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);