How to limit the length of the text inside an input text field?

Dear all, I want to limit the length of the text the user can input in an input text field. Is there a way to do so?

if not, can I detect the length of it so if it exceeds a certain length, say, 5 words, and I can make the user to input again.

thanks~

Hello Derek,

I asked this question before, so I got it working with the help of our fellow forum members.

So the code goes like this:

 -- This section happens when the user types -- Lets set up a max length, compare it and if too long, replace it local myNum = 10 -- max characters if string.len(event.text) \> myNum then -- Text too long, replace textField text with old text TextField.text = event.oldText -- change the "TextField" with the --name of your textfield print ("max length reached") end

you should put this block of code in the “editing” phase of the textbox

this code restricts any inputs when the string reached it’s limit, and goes back to the last string that the user typed before it reaches the string limit.

if you want a alert that says that the string limit has been reached call this function after the "print ( “max length reached”)

 local function checkIfBlank() local alert = native.showAlert ( "Limit reached", "I will not accept it", {"Ok"}, ) end

Hope it helps,

Goodluck,

Jam

Or you could just put

event.text = string.sub( event.text, 1, 10)
Where 10 is the max number of characters

Or if you wanted 5 words as posted above you would count the number of spaces which would be 4 if any more you would discard everything from the 5th space on

Hello Derek,

I asked this question before, so I got it working with the help of our fellow forum members.

So the code goes like this:

 -- This section happens when the user types -- Lets set up a max length, compare it and if too long, replace it local myNum = 10 -- max characters if string.len(event.text) \> myNum then -- Text too long, replace textField text with old text TextField.text = event.oldText -- change the "TextField" with the --name of your textfield print ("max length reached") end

you should put this block of code in the “editing” phase of the textbox

this code restricts any inputs when the string reached it’s limit, and goes back to the last string that the user typed before it reaches the string limit.

if you want a alert that says that the string limit has been reached call this function after the "print ( “max length reached”)

 local function checkIfBlank() local alert = native.showAlert ( "Limit reached", "I will not accept it", {"Ok"}, ) end

Hope it helps,

Goodluck,

Jam

Or you could just put

event.text = string.sub( event.text, 1, 10)
Where 10 is the max number of characters

Or if you wanted 5 words as posted above you would count the number of spaces which would be 4 if any more you would discard everything from the 5th space on

TextField.text = string.sub( event.text, 1, 10) seems to cause problems for iOS 7. I’ve also tried just setting event.text = string.sub( event.text, 1, 10), which doesn’t limit the characters in the textfield, which this used to work fine as well. Has anyone else experienced this problem?

(This is on device only, works fine on simulator)

TextField.text = string.sub( event.text, 1, 10) seems to cause problems for iOS 7. I’ve also tried just setting event.text = string.sub( event.text, 1, 10), which doesn’t limit the characters in the textfield, which this used to work fine as well. Has anyone else experienced this problem?

(This is on device only, works fine on simulator)

When I try this and uses the native keyboards undo button it crashes. Have you tried with the undo/redo buttons anything?

When I try this and uses the native keyboards undo button it crashes. Have you tried with the undo/redo buttons anything?