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'], ...]
65 initEvents: function () {
66 Ext.form.SecurePass.superclass.initEvents.call(this);
68 if (this.el.is('input[type=password]') && Roo.isSafari) {
69 this.el.on('keydown', this.SafariOnKeyDown, this);
72 this.el.on('keyup', this.checkStrength, this, {buffer: 50});
75 onRender: function (ct, position) {
76 Ext.form.SecurePass.superclass.onRender.call(this, ct, position);
77 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
78 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter ' + this.triggerClass});
80 this.trigger.createChild({
83 'margin-bottom': '10px',
84 width: this.width + 'px'
89 width: this.width + 'px',
91 'background-image': 'url(\'' + this.imageRoot + '/password_meter_grey.gif\')',
92 'background-position': 'center center',
93 'background-repeat': 'no-repeat'
101 'background-image': 'url(\'' + this.imageRoot + '/password_meter.gif\')',
102 'background-position': 'center center',
103 'background-repeat': 'no-repeat',
109 if (this.hideTrigger) {
110 this.trigger.setDisplayed(false);
112 this.setSize(this.width || '', this.height || '');
115 onDestroy: function () {
117 this.trigger.removeAllListeners();
118 this.trigger.remove();
123 Ext.form.TriggerField.superclass.onDestroy.call(this);
126 checkStrength: function () {
127 var pwd = this.el.getValue();
128 if (pwd == this._lastPwd) {
133 if (this.ClientSideStrongPassword(pwd)) {
135 } else if (this.ClientSideMediumPassword(pwd)) {
137 } else if (this.ClientSideWeakPassword(pwd)) {
142 var pm = this.trigger.child('div/div/div').dom;
144 pm.style.width = (this.width / 3) * strength + 'px';
145 //if(this.pwdStrengths != null && strength > 0) {
146 pm.innerHTML = this.meterLabel + ' ' + this.pwdStrengths[strength];
148 // pm.innerHTML = '';
154 Ext.form.SecurePass.superclass.reset.call(this);
156 var pm = this.trigger.child('div/div/div').dom;
161 validateValue: function (value) {
162 if (!Ext.form.TextField.superclass.validateValue.call(this, value)) {
165 if (value.length == 0) {
166 if (this.allowBlank) {
171 this.markInvalid(this.errors.PwdEmpty);
174 if ('[\x21-\x7e]*'.match(value)) {
175 this.markInvalid(this.errors.PwdBadChar);
178 if (value.length < 6) {
179 this.markInvalid(this.errors.PwdShort);
182 if (value.length > 16) {
183 this.markInvalid(this.errors.PwdLong);
187 if (this.ClientSideStrongPassword(value)) {
189 } else if (this.ClientSideMediumPassword(value)) {
191 } else if (this.ClientSideWeakPassword(value)) {
197 this.markInvalid(this.errors.TooWeak);
201 for (var index = 0; index < this.fieldsFilter.length; ++index) {
202 filter = document.getElementById(this.fieldsFilter[index][0]).value;
205 re = new RegExp(filter);
206 if (re.test(value)) {
207 this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
216 CharacterSetChecks: function (type) {
218 this.fResult = false;
221 isctype: function (character, type) {
222 switch (type) { //why needed break after return in js ? very odd bug
223 case this.kCapitalLetter:
224 if (character >= 'A' && character <= 'Z') {
228 case this.kSmallLetter:
229 if (character >= 'a' && character <= 'z') {
234 if (character >= '0' && character <= '9') {
238 case this.kPunctuation:
239 if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) {
249 IsLongEnough: function (pwd, size) {
250 return !(pwd == null || isNaN(size) || pwd.length < size);
253 SpansEnoughCharacterSets: function (word, nb) {
254 if (!this.IsLongEnough(word, nb))
259 var characterSetChecks = new Array(
260 new this.CharacterSetChecks(this.kCapitalLetter), new this.CharacterSetChecks(this.kSmallLetter),
261 new this.CharacterSetChecks(this.kDigit), new this.CharacterSetChecks(this.kPunctuation));
262 for (var index = 0; index < word.length; ++index) {
263 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
264 if (!characterSetChecks[nCharSet].fResult && this.isctype(word.charAt(index), characterSetChecks[nCharSet].type)) {
265 characterSetChecks[nCharSet].fResult = true;
272 for (var nCharSet = 0; nCharSet < characterSetChecks.length; ++nCharSet) {
273 if (characterSetChecks[nCharSet].fResult) {
278 if (nCharSets < nb) {
284 ClientSideStrongPassword: function (pwd) {
285 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
288 ClientSideMediumPassword: function (pwd) {
289 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
292 ClientSideWeakPassword: function (pwd) {
293 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
296 // private -- see TextFiedl... - not sure why we are duping the code?
297 SafariOnKeyDown : function(event){
299 var isSelectAll = false;
300 if(this.el.dom.selectionEnd > 0){
301 isSelectAll = (this.el.dom.selectionEnd - this.el.dom.selectionStart - this.getValue().length == 0) ? true : false;
303 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
304 event.preventDefault();
308 if(isSelectAll){ // backspace and delete key
310 event.preventDefault();
311 this.setValue(String.fromCharCode(
312 this.shiftKey ? event.getKey() : event.getKey().toLowerCase()