2 //<script type="text/Javascript">
5 Ext.form.SecurePass = function (config) {
6 // these go here, so the translation tool can replace them..
8 PwdEmpty: "Please type a password, and then retype it to confirm.",
9 PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
10 PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
11 PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
12 IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
13 FNInPwd: "Your password can't contain your first name. Please type a different password.",
14 LNInPwd: "Your password can't contain your last name. Please type a different password.",
15 TooWeak: "Your password is Too Weak."
17 this.meterLabel = "Password strength:";
18 this.pwdStrengths = ["Too Weak", "Weak", "Medium", "Strong"];
19 Ext.form.SecurePass.superclass.constructor.call(this, config);
22 Ext.extend(Ext.form.SecurePass, Ext.form.TextField, {
24 * @cfg {String/Object} errors A Error spec, or true for a default spec (defaults to
26 * PwdEmpty: "Please type a password, and then retype it to confirm.",
27 * PwdShort: "Your password must be at least 6 characters long. Please type a different password.",
28 * PwdLong: "Your password can't contain more than 16 characters. Please type a different password.",
29 * PwdBadChar: "The password contains characters that aren't allowed. Please type a different password.",
30 * IDInPwd: "Your password can't include the part of your ID. Please type a different password.",
31 * FNInPwd: "Your password can't contain your first name. Please type a different password.",
32 * LNInPwd: "Your password can't contain your last name. Please type a different password."
39 * @cfg {String/Object} Label for the strength meter (defaults to
40 * 'Password strength:')
45 * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
46 * ['Weak', 'Medium', 'Strong'])
51 * @cfg {String/Object} fieldsFilter A fieldsFilter spec, as [['field_name', 'error_id'], ...]
67 initEvents: function () {
68 Ext.form.SecurePass.superclass.initEvents.call(this);
70 if (this.el.is('input[type=password]') && Roo.isSafari) {
71 this.el.on('keydown', this.SafariOnKeyDown, this);
74 this.el.on('keyup', this.checkStrength, this, {buffer: 50});
77 onRender: function (ct, position) {
78 Ext.form.SecurePass.superclass.onRender.call(this, ct, position);
79 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
80 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
82 this.trigger.createChild({
85 'margin-bottom': '10px',
86 width: this.width + 'px'
91 width: this.width + 'px',
93 'background-image': 'url(\'' + this.imageRoot + '/password_meter_grey.gif\')',
94 'background-position': 'center center',
95 'background-repeat': 'no-repeat'
103 'background-image': 'url(\'' + this.imageRoot + '/password_meter.gif\')',
104 'background-position': 'center center',
105 'background-repeat': 'no-repeat',
111 if (this.hideTrigger) {
112 this.trigger.setDisplayed(false);
114 this.setSize(this.width || '', this.height || '');
117 onDestroy: function () {
119 this.trigger.removeAllListeners();
120 this.trigger.remove();
125 Ext.form.TriggerField.superclass.onDestroy.call(this);
128 checkStrength: function () {
129 var pwd = this.el.getValue();
130 if (pwd == this._lastPwd) {
135 if (this.ClientSideStrongPassword(pwd)) {
137 } else if (this.ClientSideMediumPassword(pwd)) {
139 } else if (this.ClientSideWeakPassword(pwd)) {
144 var pm = this.trigger.child('div/div/div').dom;
146 pm.style.width = (this.width / 3) * strength + 'px';
147 //if(this.pwdStrengths != null && strength > 0) {
148 pm.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
150 // pm.innerHTML = '';
156 Ext.form.SecurePass.superclass.reset.call(this);
158 var pm = this.trigger.child('div/div/div').dom;
163 validateValue: function (value) {
165 if (!Ext.form.TextField.superclass.validateValue.call(this, value)) {
168 if (value.length == 0) {
169 if (this.allowBlank) {
174 this.markInvalid(this.errors.PwdEmpty);
182 if ('[\x21-\x7e]*'.match(value)) {
183 this.markInvalid(this.errors.PwdBadChar);
186 if (value.length < 6) {
187 this.markInvalid(this.errors.PwdShort);
190 if (value.length > 16) {
191 this.markInvalid(this.errors.PwdLong);
195 if (this.ClientSideStrongPassword(value)) {
197 } else if (this.ClientSideMediumPassword(value)) {
199 } else if (this.ClientSideWeakPassword(value)) {
205 this.markInvalid(this.errors.TooWeak);
209 for (var index = 0; index < this.fieldsFilter.length; ++index) {
210 filter = document.getElementById(this.fieldsFilter[index][0]).value;
213 re = new RegExp(filter);
214 if (re.test(value)) {
215 this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
224 CharacterSetChecks: function (type) {
226 this.fResult = false;
229 isctype: function (character, type) {
230 switch (type) { //why needed break after return in js ? very odd bug
231 case this.kCapitalLetter:
232 if (character >= 'A' && character <= 'Z') {
236 case this.kSmallLetter:
237 if (character >= 'a' && character <= 'z') {
242 if (character >= '0' && character <= '9') {
246 case this.kPunctuation:
247 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
257 IsLongEnough: function (pwd, size) {
258 return !(pwd == null || isNaN(size) || pwd.length < size);
261 SpansEnoughCharacterSets: function (word, nb) {
262 if (!this.IsLongEnough(word, nb))
267 var characterSetChecks = new Array(
268 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
269 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
270 for (var index = 0; index < word.length; ++index) {
271 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
272 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
273 characterSetChecks[nCharSet].fResult = true;
280 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
281 if (characterSetChecks[nCharSet].fResult) {
286 if (nCharSets < nb) {
292 ClientSideStrongPassword: function (pwd) {
293 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
296 ClientSideMediumPassword: function (pwd) {
297 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
300 ClientSideWeakPassword: function (pwd) {
301 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
304 // private -- see TextFiedl... - not sure why we are duping the code?
305 SafariOnKeyDown : function(event){
307 var isSelectAll = false;
308 if(this.el.dom.selectionEnd > 0){
309 isSelectAll = (this.el.dom.selectionEnd - this.el.dom.selectionStart - this.getValue().length == 0) ? true : false;
311 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
312 event.preventDefault();
316 if(isSelectAll){ // backspace and delete key
318 event.preventDefault();
319 this.setValue(String.fromCharCode(
320 this.shiftKey ? event.getKey() : event.getKey().toLowerCase()