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 = ["tooweak", "weak", "medium", "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({
97 'margin-bottom': '20px',
98 width: this.meterWidth + 'px'
102 cls: 'password-meter-grey',
104 width: this.meterWidth + 'px',
111 cls: 'password-meter',
114 //width: this.meterWidth + 'px',
119 cls: 'password-meter-text',
125 if (this.hideTrigger) {
126 this.trigger.setDisplayed(false);
128 this.setSize(this.width || '', this.height || '');
131 onDestroy: function () {
133 this.trigger.removeAllListeners();
134 this.trigger.remove();
139 Roo.bootstrap.TriggerField.superclass.onDestroy.call(this);
142 checkStrength: function () {
143 var pwd = this.inputEl().getValue();
144 if (pwd == this._lastPwd) {
149 if (this.ClientSideStrongPassword(pwd)) {
151 } else if (this.ClientSideMediumPassword(pwd)) {
153 } else if (this.ClientSideWeakPassword(pwd)) {
158 var pm = this.trigger.child('div/div/div').dom;
159 console.log('strength1: ' + strength);
161 pm.style.width = (this.meterWidth / 3) * strength + 'px';
162 //pm.style.class = 'password-meter' + meterClass[strength];
164 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
166 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
171 Roo.bootstrap.SecurePass.superclass.reset.call(this);
174 var pm = this.trigger.child('div/div/div').dom;
175 //pm.style.width = 0;
177 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
179 //pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
182 this.inputEl().dom.type='password';
185 validateValue: function (value) {
187 if (!Roo.bootstrap.SecurePass.superclass.validateValue.call(this, value)) {
190 if (value.length == 0) {
191 if (this.allowBlank) {
196 this.markInvalid(this.errors.PwdEmpty);
197 this.errorMsg = this.errors.PwdEmpty;
205 if ('[\x21-\x7e]*'.match(value)) {
206 this.markInvalid(this.errors.PwdBadChar);
207 this.errorMsg = this.errors.PwdBadChar;
210 if (value.length < 6) {
211 this.markInvalid(this.errors.PwdShort);
212 this.errorMsg = this.errors.PwdShort;
215 if (value.length > 16) {
216 this.markInvalid(this.errors.PwdLong);
217 this.errorMsg = this.errors.PwdLong;
221 if (this.ClientSideStrongPassword(value)) {
223 } else if (this.ClientSideMediumPassword(value)) {
225 } else if (this.ClientSideWeakPassword(value)) {
233 this.markInvalid(this.errors.TooWeak);
234 this.errorMsg = this.errors.TooWeak;
239 console.log('strength2: ' + strength);
240 var pm = this.trigger.child('div/div/div').dom;
242 pm.style.width = (this.meterWidth / 3) * strength + 'px';
243 //pm.style.class = 'password-meter' + meterClass[strength];
245 //pm.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
247 var pt = this.trigger.child('/div/div/').child('>*[class=password-meter-text]').dom;
249 pt.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
254 CharacterSetChecks: function (type) {
256 this.fResult = false;
259 isctype: function (character, type) {
260 switch (type) { //why needed break after return in js ? very odd bug
261 case this.kCapitalLetter:
262 if (character >= 'A' && character <= 'Z') {
266 case this.kSmallLetter:
267 if (character >= 'a' && character <= 'z') {
272 if (character >= '0' && character <= '9') {
276 case this.kPunctuation:
277 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
287 IsLongEnough: function (pwd, size) {
288 return !(pwd == null || isNaN(size) || pwd.length < size);
291 SpansEnoughCharacterSets: function (word, nb) {
292 if (!this.IsLongEnough(word, nb))
297 var characterSetChecks = new Array(
298 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
299 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
300 for (var index = 0; index < word.length; ++index) {
301 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
302 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
303 characterSetChecks[nCharSet].fResult = true;
310 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
311 if (characterSetChecks[nCharSet].fResult) {
316 if (nCharSets < nb) {
322 ClientSideStrongPassword: function (pwd) {
323 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
326 ClientSideMediumPassword: function (pwd) {
327 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
330 ClientSideWeakPassword: function (pwd) {
331 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);