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."
41 * @cfg {String/Object} Label for the strength meter (defaults to
42 * 'Password strength:')
48 * @cfg {String/Object} pwdStrengths A pwdStrengths spec, or true for a default spec (defaults to
49 * ['Weak', 'Medium', 'Strong'])
55 * @cfg {String/Object} fieldsFilter A fieldsFilter spec, as [['field_name', 'error_id'], ...]
73 initEvents : function(){
75 if(this.el.is('input[type=password]') && Roo.isSafari){
76 this.el.on('keydown', this.SafariOnKeyDown, this);
77 this.el.on("focus", function(){
81 Ext.form.SecurePass.superclass.initEvents.call(this);
82 this.el.on('keyup', this.checkStrength, this, {buffer:50});
86 onRender : function(ct, position){
87 Ext.form.SecurePass.superclass.onRender.call(this, ct, position);
88 this.wrap = this.el.wrap({cls: 'x-form-field-wrap'});
89 this.trigger = this.wrap.createChild({tag: 'div', cls: 'StrengthMeter '+this.triggerClass});
91 this.trigger.createChild({
94 'margin-bottom': '10px',
95 width: this.width + 'px'
100 width: this.width + 'px',
102 'background-image' : 'url(\''+this.imageRoot+'/password_meter_grey.gif\')',
103 'background-position' : 'center center',
104 'background-repeat': 'no-repeat'
112 'background-image': 'url(\''+this.imageRoot+'/password_meter.gif\')',
113 'background-position': 'center center',
114 'background-repeat': 'no-repeat',
120 if(this.hideTrigger){
121 this.trigger.setDisplayed(false);
123 this.setSize(this.width||'', this.height||'');
127 onDestroy : function(){
129 this.trigger.removeAllListeners();
130 this.trigger.remove();
135 Ext.form.TriggerField.superclass.onDestroy.call(this);
139 checkStrength : function(){
140 var pwd = this.el.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;
157 pm.style.width = (this.width/3) * strength +'px';
158 //if(this.pwdStrengths != null && strength > 0) {
159 pm.innerHTML = this.meterLabel + ' '+ this.pwdStrengths[strength];
161 // pm.innerHTML = '';
167 Ext.form.SecurePass.superclass.reset.call(this);
169 var pm = this.trigger.child('div/div/div').dom;
174 validateValue : function(value){
175 if (!Ext.form.TextField.superclass.validateValue.call(this, value)){
178 if (value.length == 0) {
179 if (this.allowBlank) {
184 this.markInvalid(this.errors.PwdEmpty);
187 if ('[\x21-\x7e]*'.match(value)) {
188 this.markInvalid(this.errors.PwdBadChar);
191 if (value.length < 6) {
192 this.markInvalid(this.errors.PwdShort);
195 if (value.length > 16) {
196 this.markInvalid(this.errors.PwdLong);
200 if (this.ClientSideStrongPassword(value)) {
202 } else if(this.ClientSideMediumPassword(value)) {
204 } else if(this.ClientSideWeakPassword(value)) {
210 this.markInvalid(this.errors.TooWeak);
214 for (var index = 0; index < this.fieldsFilter.length; ++index) {
215 filter = document.getElementById(this.fieldsFilter[index][0]).value;
218 re = new RegExp(filter);
219 if (re.test(value)) {
220 this.markInvalid(eval('this.errors.'+ this.fieldsFilter[index][1]));
230 CharacterSetChecks : function(type){
232 this.fResult = false;
236 isctype : function(character, type){
237 switch (type) { //why needed break after return in js ? very odd bug
238 case this.kCapitalLetter: if (character >= 'A' && character <= 'Z') { return true; } break;
239 case this.kSmallLetter: if (character >= 'a' && character <= 'z') { return true; } break;
240 case this.kDigit: if (character >= '0' && character <= '9') { return true; } break;
241 case this.kPunctuation: if ('!@#$%^&*()_+-=\'";:[{]}|.>,</?`~'.indexOf(character) >= 0) { return true; } break;
242 default: return false;
248 IsLongEnough : function(pwd, size){
249 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) {
285 ClientSideStrongPassword : function(pwd){
286 return this.IsLongEnough(pwd, 8) && this.SpansEnoughCharacterSets(pwd, 3);
290 ClientSideMediumPassword : function(pwd){
291 return this.IsLongEnough(pwd, 7) && this.SpansEnoughCharacterSets(pwd, 2);
295 ClientSideWeakPassword : function(pwd){
296 return this.IsLongEnough(pwd, 6) || !this.IsLongEnough(pwd, 0);
300 SafariOnKeyDown : function(event){
301 if((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1){ // backspace and delete key
302 event.preventDefault();