-
28 Mar 2008 3:16 PM #1
TextField AllowBlank to trim spaces
TextField AllowBlank to trim spaces
Couldn't find this anywhere, so I thought I'd post it... The AllowBlank parameter on TextFields doesn't trim spaces before validating, and as such would allow empty values. This will make a global validator in such cases:
Code:Ext.apply(Ext.form.TextField.prototype,{ validator:function(text){ if(this.allowBlank==false && Ext.util.Format.trim(text).length==0) return false; else return true; } });
-
23 May 2008 2:07 AM #2
thanks it works very well...
I think its better to use Ext.override instead of Ext.apply
PHP Code:Ext.override(Ext.form.TextField, {
validator:function(text){
if(this.allowBlank==false && Ext.util.Format.trim(text).length==0)
return false;
else
return true;
}
});
-
16 Feb 2009 11:01 PM #3
hi tarini,
How can I use this? suppose I hava a textfield:
PHP Code:NameField = new Ext.form.TextField({
id: 'NameField',
fieldLabel: 'Name',
maxLength: 20,
allowBlank: false,
anchor : '95%',
itemCls: 'required',
maskRe: /([a-zA-Z0-9\s]+)$/
});
-
17 Feb 2009 3:48 AM #4Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 41
Wouldn't it be easier to simply use
in the TextField config?Code:stripCharsRe: /(^\s+|\s+$)/g
(this strips leading and trailing spaces from the result, so this also changes the value!)
-
9 Apr 2009 10:39 AM #5
Is there a way to add a delay period before the stripping of chars (in this case spaces) occurs (ex: wait until end-user stops typing)?
-
9 Apr 2009 10:48 AM #6
Nevermind...
Added validationDelay: config option to my TextField and it worked with no problem!
-
27 Apr 2009 1:48 AM #7
Good to know about the validationDelay. Its really required to improve the User Experience.
Thanks
-
18 Jan 2010 5:49 AM #8
@ extjs_new
If you need this for all fields, not only those with allowblank=false then use this:
Validator receives true when field contains only blanksCode:Ext.override(Ext.form.TextField, { validator:function(text){ return (text.length==0 || Ext.util.Format.trim(text).length!=0); } });
This will be valid for all textfields you define, you don't need to enter any other property.
The text.length==0 tests if the field isn't empty. If the field is not empty, but after trimming is empty then you have blanks so the validator receives false.


Reply With Quote