Titanium Community Questions & Answer Archive

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

How to validate input in a text field ?


I'm creating an application with some text field which are supposed to contain only floating point numbers. How can I prevent the user from typing too many characters in such field ?

In typical web apps, one would register for the key press event and stop unwanted events. I don't see a similar mechanism in Appcelerator.


— asked August 3rd 2010 by Olivier Smeesters
  • input
  • mobile
  • textfield
  • validation

2 Answers

  • I do it like this:

    the_input.addEvent('change', function(e){
                e.source.value = e.source.value.slice(0,the_limit);

    it will allow you to enter only what the_limit allows ( let's say 10 )

    — answered August 3rd 2010 by Dan Tamas
    1 Comment
    • Aren't you going to get an infinite recursion here when the change handler changes the field value ?

      — commented August 3rd 2010 by Olivier Smeesters
  • I have written a validator library for my app which takes a lot free form data. Due to the type of data and form design I cat the data a the end and present a alert dialogue with information on the problems.

    I might change this in future and check on change event firing.

    — answered August 3rd 2010 by Nick The Geek
    1 Comment
    • This appears to answer my question "Does Titanium contain a form field validation API?" Answer = no. It makes absolutely no sense to me why this is so. Applications are fundamentally forms and reports. Validation is an integral part of data input. Guess I have to roll my own.

      — commented August 9th 2011 by Michael Stelly
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.