Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

TextField: limit allowed characters?

Is it possible to limit user input in a textfield? For example, I'd like to allow only numbers in a particular field.

— asked September 1st 2010 by Parand Darugar
  • mobile
0 Comments

2 Answers

  • Accepted Answer

    I have this on the change event, to restrict the input to numbers and under a certain limit

    customer_phone_limit =  10;
    

    the change event triggers this function

    function(e) {
            var val = phone.value;
            !!( /[^0-9]/.test(val) ) ? phone.value = val.replace(/[^0-9]/gi,'') ): false ;
            e.source.value = e.source.value.slice(0,customer_phone_limit);
    }
    
    — answered September 2nd 2010 by Dan Tamas
    permalink
    1 Comment
    • That works well for me thank you. On android it prepositions the cursor to the start of the field, but not a huge issue, the user shouldn't be pressing the wrong buttons anyway :)

      I changed it a little to make it easier to just copy/paste to all of my textfield events

      var val = e.source.value;
      !!( /[^0-9]/.test(val) ) ? e.source.value = val.replace(/[^0-9]/gi,'') : false;
      

      — commented July 20th 2013 by Adam Parker
  • if you are only wanting numbers in a textfield, then you should just bring up the number keyboard instead of the regular keyboard. that way there's no way for someone to type in anything other than numbers.

    rocksteady,
    danno~

    — answered September 2nd 2010 by danno watts
    permalink
    1 Comment
    • I don't think that's true - you can hit the "ABC" key, at least on Android, to switch over to an alphabet keyboard.

      — commented September 2nd 2010 by Parand Darugar
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.