Hybrid View

  1. #1
    Ext JS Premium Member
    Join Date
    Jan 2008
    Posts
    4
    Vote Rating
    0
    dviry is on a distinguished road

      0  

    Default 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;
        }
    });

  2. #2
    Ext User
    Join Date
    Dec 2007
    Location
    italy
    Posts
    120
    Vote Rating
    0
    tarini is on a distinguished road

      0  

    Default


    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;
        }
    }); 

  3. #3
    Ext User
    Join Date
    Dec 2008
    Posts
    108
    Vote Rating
    0
    extjs_new is on a distinguished road

      0  

    Default


    hi tarini,

    How can I use this? suppose I hava a textfield:
    PHP Code:
     NameField = new Ext.form.TextField({
        
    id'NameField',
        
    fieldLabel'Name',    
        
    maxLength20,
        
    allowBlankfalse,    
        
    anchor '95%',   
        
    itemCls'required',
        
    maskRe: /([a-zA-Z0-9\s]+)$/  
          }); 

  4. #4
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    92
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      2  

    Default


    Wouldn't it be easier to simply use
    Code:
    stripCharsRe: /(^\s+|\s+$)/g
    in the TextField config?

    (this strips leading and trailing spaces from the result, so this also changes the value!)

  5. #5
    Sencha User
    Join Date
    Dec 2007
    Posts
    11
    Vote Rating
    0
    vtmoren is on a distinguished road

      0  

    Default


    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)?

  6. #6
    Sencha User
    Join Date
    Dec 2007
    Posts
    11
    Vote Rating
    0
    vtmoren is on a distinguished road

      0  

    Default


    Nevermind...
    Added validationDelay: config option to my TextField and it worked with no problem!

  7. #7
    Ext User
    Join Date
    Apr 2009
    Posts
    7
    Vote Rating
    0
    Acmeous is on a distinguished road

      0  

    Default


    Good to know about the validationDelay. Its really required to improve the User Experience.
    Thanks

  8. #8
    Sencha User
    Join Date
    Oct 2009
    Posts
    18
    Vote Rating
    0
    riuslex is on a distinguished road

      0  

    Thumbs up


    @ extjs_new

    If you need this for all fields, not only those with allowblank=false then use this:

    Code:
    Ext.override(Ext.form.TextField, {
        validator:function(text){
            return (text.length==0 || Ext.util.Format.trim(text).length!=0);
        }
    });
    Validator receives true when field contains only blanks

    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.