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."
55 * @cfg {String/Object} Label for the strength meter (defaults to
56 * 'Password strength:')
61 * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
62 * ['Weak', 'Medium', 'Strong'])
78 initEvents: function () {
79 Roo.bootstrap.SecurePass.superclass.initEvents.call(this);
81 if (this.el.is('input[type=password]') && Roo.isSafari) {
82 this.el.on('keydown', this.SafariOnKeyDown, this);
85 this.el.on('keyup', this.checkStrength, this, {buffer: 50});
88 onRender: function (ct, position) {
89 Roo.bootstrap.SecurePass.superclass.onRender.call(this, ct, position);
90 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
91 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
93 this.trigger.createChild({
96 'margin-bottom': '20px',
97 width: this.meterWidth + 'px'
101 cls: 'password-meter-grey',
103 width: this.meterWidth + 'px',
110 cls: 'password-meter',
117 cls: 'password-meter-text',
123 if (this.hideTrigger) {
124 this.trigger.setDisplayed(false);
126 this.setSize(this.width || '', this.height || '');
129 onDestroy: function () {
131 this.trigger.removeAllListeners();
132 this.trigger.remove();
137 Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
140 checkStrength: function () {
141 var pwd = this.inputEl().getValue();
142 if (pwd == this._lastPwd) {
147 if (this.ClientSideStrongPassword(pwd)) {
149 } else if (this.ClientSideMediumPassword(pwd)) {
151 } else if (this.ClientSideWeakPassword(pwd)) {
156 var pm = this.trigger.child('div/div/div').dom;
157 console.log('strength1: ' + strength);
159 pm.style.width = (this.meterWidth / 3) * strength + 'px';
161 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
163 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
168 Roo.bootstrap.SecurePass.superclass.reset.call(this);
171 var pm = this.trigger.child('div/div/div').dom;
174 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
176 //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
179 this.inputEl().dom.type='password';
182 validateValue: function (value) {
184 if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
187 if (value.length == 0) {
188 if (this.allowBlank) {
193 this.markInvalid(this.errors.PwdEmpty);
201 if ('[\x21-\x7e]*'.match(value)) {
202 this.markInvalid(this.errors.PwdBadChar);
205 if (value.length < 6) {
206 this.markInvalid(this.errors.PwdShort);
209 if (value.length > 16) {
210 this.markInvalid(this.errors.PwdLong);
214 if (this.ClientSideStrongPassword(value)) {
216 } else if (this.ClientSideMediumPassword(value)) {
218 } else if (this.ClientSideWeakPassword(value)) {
225 this.markInvalid(this.errors.TooWeak);
229 console.log('strength2: ' + strength);
230 var pm = this.trigger.child('div/div/div').dom;
232 pm.style.width = (this.meterWidth / 3) * strength + 'px';
234 //pm.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
236 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
238 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
243 CharacterSetChecks: function (type) {
245 this.fResult = false;
248 isctype: function (character, type) {
249 switch (type) { //why needed break after return in js ? very odd bug
250 case this.kCapitalLetter:
251 if (character >= 'A' && character <= 'Z') {
255 case this.kSmallLetter:
256 if (character >= 'a' && character <= 'z') {
261 if (character >= '0' && character <= '9') {
265 case this.kPunctuation:
266 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
276 IsLongEnough: function (pwd, size) {
277 return !(pwd == null || isNaN(size) || pwd.length < size);
280 SpansEnoughCharacterSets: function (word, nb) {
281 if (!this.IsLongEnough(word, nb))
286 var characterSetChecks = new Array(
287 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
288 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
289 for (var index = 0; index < word.length; ++index) {
290 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
291 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
292 characterSetChecks[nCharSet].fResult = true;
299 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
300 if (characterSetChecks[nCharSet].fResult) {
305 if (nCharSets < nb) {
311 ClientSideStrongPassword: function (pwd) {
312 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
315 ClientSideMediumPassword: function (pwd) {
316 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
319 ClientSideWeakPassword: function (pwd) {
320 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);