Results 1 to 9 of 9

Thread: TextField AllowBlank to trim spaces

  1. #1
    Ext JS Premium Member
    Join Date
    Jan 2008
    Posts
    4
    Vote Rating
    0
      0  

    Default 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
      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
      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 User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    130
      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
      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
      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
      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
      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.

  9. #9
    Sencha User jeanphi-jnesis's Avatar
    Join Date
    Jul 2014
    Location
    New Zealand
    Posts
    5
    Vote Rating
    0
      0  

    Default

    The solution proposed by Condor is interesting and will work if you don't allow words containing whitespaces.
    Because it applies while you are typing it will remove any space you want to put in the middle of a word,unless you write the word with no space and add them in the end...which is not very user friendly.
    maskRe has a similar behaviour except it doesn't remove the unwanted characters, it just doesn't let you type them. This solution also prevent typing a whitespace in the middle of a word.


    The only solution I see in the case you allow spaces in the middle is to let the user type the whitespace.
    Then you have to decide whether:


    • You prefer to mark invalid the field, this could be nice to explain why the user can't type whitespaces:

    adding a validator will work but I prefer adding regex:
    Code:
    /^\S.*\S$|^\S$/
    and
    Code:
    regexText: 'This field doesn't allow trailing or leading whitespaces'


    • Or, you prefer to not mark this on error and then trim() the result just after the value has been input.
    Jean-Philippe Ehret
    Founder of Jnesis
    http://www.jnesis.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •