TextField with limited length

Hello everyone,
I was wondering, if anyone knows a good way how to limit a user in including too much characters in TextField.

I am trying to use a listener function, where I am controlling the length of inserted string so far. When I detect limit length, I would like to deny TextField from appending new characters. But I am not able to do it. New characters are inserted everytime. (user can either type or paste text from clipboard). Here is some code:

local maxChars = 100  
  
local function inputEventHandler(event)  
 if event.phase == "editing" then  
 if event.oldString:len() \>= maxChars and event.newCharacters:len() \> 0 then  
 -- length of oldString is maximal possible and inserting  
 -- of new characters is detected - lets cut the rest  
 -- Unfortunately, textField.text does not contain new characters yet, so this does not help.  
 textField.text = event.target.text:sub(1, maxChars)  
  
 event.newCharacters = "" -- this is not working of course, but I tried it :)  
 return true -- this also does not prevent TextField from inserting new chars  
 end  
 end  
end  
  
textField = native.newTextField( 100, 100, 100, 30, inputEventHandler)  

This works not wery well, new characters are always appended, which is not what I want.

Any suggestion will be very appreciated. Thank you.
Radim [import]uid: 52745 topic_id: 12345 reply_id: 312345[/import]

in the “began” event.phase add an enterFrame listener which restricts the number of characters to maxChars

textField.text=string.sub(textField.text,1,maxChars)  

in the “submitted” event.phase remove the enterFrame listener

[import]uid: 6459 topic_id: 12345 reply_id: 45113[/import]

Hi tetu, is there any way you could provide sample working code on your above suggestion? You mention to include an enterFrame listener in the “began” event and remove the enterFrame listener in the “submitted” event. Would you be able to show us your code for that? Thanks so much, I can’t figure this out! [import]uid: 82194 topic_id: 12345 reply_id: 66597[/import]

try this

[lua]-- TextField Listener
local function fieldHandler( getObj )

– Use Lua closure in order to access the TextField object

return function( event )

print( "TextField Object is: " … tostring( getObj() ) )

if ( “began” == event.phase ) then
– This is the “keyboard has appeared” event
function chknum()
defaultField.text = string.sub(defaultField.text,1,6)
end
Runtime:addEventListener(“enterFrame”,chknum)

elseif ( “ended” == event.phase ) then
– This event is called when the user stops editing a field:
– for example, when they touch a different field or keyboard focus goes away

print( "Text entered = " … tostring( getObj().text ) ) – display the text entered

elseif ( “submitted” == event.phase ) then
– This event occurs when the user presses the “return” key
– (if available) on the onscreen keyboard

– Hide keyboard
Runtime:removeEventListener(“enterFrame”,chknum)
native.setKeyboardFocus( nil )
end

end – “return function()”

end

– Create our Text Field
defaultField = native.newTextField( 10, 30, 180, 30,
fieldHandler( function() return defaultField end ) )[/lua]
:slight_smile: [import]uid: 12482 topic_id: 12345 reply_id: 66602[/import]

hgvyas123 – WOW!! It worked! You’re the best, thank you SO much for the code. I owe ya one! =) [import]uid: 82194 topic_id: 12345 reply_id: 66608[/import]

thanks dude
:slight_smile: [import]uid: 12482 topic_id: 12345 reply_id: 66609[/import]

i just stumbled upon this thread, here is an alternative with out a runtime

[code]
if (“editing” == event.phase ) then

local myText = getObj().text

myText = myText:sub(1,3) --max three letters
myText = string.lower(myText ) --lower case

getObj().text = myText
end

[/code] [import]uid: 89663 topic_id: 12345 reply_id: 106250[/import]

Also stumbling on this thread…

For anyone ever looking at this later…
There is an alt way to do this

 --- this is based off of the sample text on corona.  
 if ( "editing" == event.phase) then  
  
 if(string.len(defaultField.text)) \>7 then  
 defaultField.text = defaultField.text:sub(1,7)  
 end  
 end  

[import]uid: 24708 topic_id: 12345 reply_id: 112582[/import]

This api call generates a events every key press:

if event.phase == “editing” then …

You can check during that phase if string.len(field.text) is too long and make your limits that way.

Prior to discovering this, I setup an enterFrame listener that checked the length of the string and showed the user how many characters were left.

[import]uid: 19626 topic_id: 12345 reply_id: 112591[/import]