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."
54 * @cfg {String/Object} Label for the strength meter (defaults to
55 * 'Password strength:')
60 * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
61 * ['Weak', 'Medium', 'Strong'])
77 initEvents: function () {
78 Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
80 if (this.el.is('input[type=password]') && Roo.isSafari) {
81 this.el.on('keydown', this.SafariOnKeyDown, this);
84 this.el.on('keyup', this.checkStrength, this, {buffer: 50});
87 onRender: function (ct, position) {
88 Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
89 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
90 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
92 this.trigger.createChild({
95 'margin-bottom': '20px',
96 width: this.meterWidth + 'px'
100 cls: 'password-meter-grey',
102 width: this.meterWidth + 'px',
109 cls: 'password-meter',
116 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)) {
155 var pm = this.trigger.child('div/div/div').dom;
156 console.log('strength1: ' + strength);
158 pm.style.width = (this.meterWidth / 3) * strength + 'px';
160 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
162 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
167 Roo.bootstrap.SecurePass.superclass.reset.call(this);
170 var pm = this.trigger.child('div/div/div').dom;
173 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
175 //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
180 validateValue: function (value) {
182 if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
185 if (value.length == 0) {
186 if (this.allowBlank) {
191 this.markInvalid(this.errors.PwdEmpty);
199 if ('[\x21-\x7e]*'.match(value)) {
200 this.markInvalid(this.errors.PwdBadChar);
203 if (value.length < 6) {
204 this.markInvalid(this.errors.PwdShort);
207 if (value.length > 16) {
208 this.markInvalid(this.errors.PwdLong);
212 if (this.ClientSideStrongPassword(value)) {
214 } else if (this.ClientSideMediumPassword(value)) {
216 } else if (this.ClientSideWeakPassword(value)) {
223 this.markInvalid(this.errors.TooWeak);
226 console.log('strength2: ' + strength);
227 var pm = this.trigger.child('div/div/div').dom;
229 pm.style.width = (this.meterWidth / 3) * strength + 'px';
231 //pm.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
233 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
235 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
240 CharacterSetChecks: function (type) {
242 this.fResult = false;
245 isctype: function (character, type) {
246 switch (type) { //why needed break after return in js ? very odd bug
247 case this.kCapitalLetter:
248 if (character >= 'A' && character <= 'Z') {
252 case this.kSmallLetter:
253 if (character >= 'a' && character <= 'z') {
258 if (character >= '0' && character <= '9') {
262 case this.kPunctuation:
263 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
273 IsLongEnough: function (pwd, size) {
274 return !(pwd == null || isNaN(size) || pwd.length < size);
277 SpansEnoughCharacterSets: function (word, nb) {
278 if (!this.IsLongEnough(word, nb))
283 var characterSetChecks = new Array(
284 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
285 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
286 for (var index = 0; index < word.length; ++index) {
287 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
288 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
289 characterSetChecks[nCharSet].fResult = true;
296 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
297 if (characterSetChecks[nCharSet].fResult) {
302 if (nCharSets < nb) {
308 ClientSideStrongPassword: function (pwd) {
309 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
312 ClientSideMediumPassword: function (pwd) {
313 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
316 ClientSideWeakPassword: function (pwd) {
317 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);