newTextField max # of characters

I coulda sworn someone asked the same question in the forums just a few days ago, and got a response, and now I can’t find it.

How do I limit the max # of characters the user can input in a native text field? Must be something you do with the listener on “editing” phase, but I tried all kinds of stuff and no go. I can check what the event.text length is, and if it is higher than the max, set the text field’s text to event.oldText, but that moves the caret to the beginning and that’s not desirable.

Help… [import]uid: 160496 topic_id: 29895 reply_id: 329895[/import]

I created a function that checks the length of text string, and if the length is longer than the desired length, it would notify the user.

BTW, if you want to limit it as the user is entering the text, I don’t know how to do it. I only do the test upon text submission (i.e., at ended phase.)

Naomi

Edit : I’m brainstorming, but I wonder if you could add something like native.setKeyboardFocus( nil ) in “editing” phase when the length of text string reaches max character… [import]uid: 67217 topic_id: 29895 reply_id: 119905[/import]

Looking at the documention gives a few clues on how to do this. I’ll use their example with some changes to illustrate how I handled this in the past. In your listener for the textfield you want to check the phase for “editing”.

[code]
local defaultField

local function textListener( event )
if event.phase == “began” then

elseif event.phase == “ended” then

elseif event.phase == “editing” then

– 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
defaultField.text = event.oldText
end

end
end

– Create our Text Field
defaultField = native.newTextField( 10, 30, 180, 30 )
defaultField.userInput = textListener
defaultField:addEventListener( “userInput”, defaultField )
[/code] [import]uid: 56820 topic_id: 29895 reply_id: 119932[/import]

As I pointed out in the OP, I tried that, but that method resets the caret to the beginning of the test, whis is not the desired effect. [import]uid: 160496 topic_id: 29895 reply_id: 119952[/import]

Yeah I see, I thought it was a different thread. This has been asked a couple times lately.

As for the caret resetting. I don’t experience that issue on simulator (Mac) or iOS devices. Is this an android only issue? [import]uid: 56820 topic_id: 29895 reply_id: 119953[/import]

Maybe. My kid took my iPad away from e for a week so I didn’t try it on iOS. [import]uid: 160496 topic_id: 29895 reply_id: 119955[/import]

Dupe [import]uid: 160496 topic_id: 29895 reply_id: 119956[/import]

I actually pulled up the code I used in my last app. I actually do it a little different but it’s essentially the same concept. Perhaps it makes a difference though. I am acutally using a substring of the text rather then event.oldText.

[code]
function GetInput(event)
if event.phase == “submitted” and pbox.isVisible == true then

elseif event.phase == “editing” and pbox.isVisible == true then
local ptxt = pbox.text
ptxt = string.gsub(ptxt, “[^%w%s]”, “”)
if string.len(ptxt) > 12 then ptxt = string.sub(ptxt,0,12) end
pbox.text = ptxt
return true
end
end
[/code] [import]uid: 56820 topic_id: 29895 reply_id: 119957[/import]

@anderoth, thanks for posting how you did it. It looks great. I might use it too.

Cheers,
Naomi [import]uid: 67217 topic_id: 29895 reply_id: 119961[/import]

I created a function that checks the length of text string, and if the length is longer than the desired length, it would notify the user.

BTW, if you want to limit it as the user is entering the text, I don’t know how to do it. I only do the test upon text submission (i.e., at ended phase.)

Naomi

Edit : I’m brainstorming, but I wonder if you could add something like native.setKeyboardFocus( nil ) in “editing” phase when the length of text string reaches max character… [import]uid: 67217 topic_id: 29895 reply_id: 119905[/import]

Looking at the documention gives a few clues on how to do this. I’ll use their example with some changes to illustrate how I handled this in the past. In your listener for the textfield you want to check the phase for “editing”.

[code]
local defaultField

local function textListener( event )
if event.phase == “began” then

elseif event.phase == “ended” then

elseif event.phase == “editing” then

– 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
defaultField.text = event.oldText
end

end
end

– Create our Text Field
defaultField = native.newTextField( 10, 30, 180, 30 )
defaultField.userInput = textListener
defaultField:addEventListener( “userInput”, defaultField )
[/code] [import]uid: 56820 topic_id: 29895 reply_id: 119932[/import]

As I pointed out in the OP, I tried that, but that method resets the caret to the beginning of the test, whis is not the desired effect. [import]uid: 160496 topic_id: 29895 reply_id: 119952[/import]

Yeah I see, I thought it was a different thread. This has been asked a couple times lately.

As for the caret resetting. I don’t experience that issue on simulator (Mac) or iOS devices. Is this an android only issue? [import]uid: 56820 topic_id: 29895 reply_id: 119953[/import]

Maybe. My kid took my iPad away from e for a week so I didn’t try it on iOS. [import]uid: 160496 topic_id: 29895 reply_id: 119955[/import]

Dupe [import]uid: 160496 topic_id: 29895 reply_id: 119956[/import]

I actually pulled up the code I used in my last app. I actually do it a little different but it’s essentially the same concept. Perhaps it makes a difference though. I am acutally using a substring of the text rather then event.oldText.

[code]
function GetInput(event)
if event.phase == “submitted” and pbox.isVisible == true then

elseif event.phase == “editing” and pbox.isVisible == true then
local ptxt = pbox.text
ptxt = string.gsub(ptxt, “[^%w%s]”, “”)
if string.len(ptxt) > 12 then ptxt = string.sub(ptxt,0,12) end
pbox.text = ptxt
return true
end
end
[/code] [import]uid: 56820 topic_id: 29895 reply_id: 119957[/import]

@anderoth, thanks for posting how you did it. It looks great. I might use it too.

Cheers,
Naomi [import]uid: 67217 topic_id: 29895 reply_id: 119961[/import]