I have used text field to store username and want to restrict user name should contain maximum 7 character. I expect native text field should not accept more than 7 character.. is there any way to do that?
Label: corona android iphone ipad building error inmobi problem display screen View image tableView failed object physics touch button game lua config event listener |
How to restrict number of character in Native text field
You could add a listener; http://developer.anscamobile.com/reference/index/nativenewtextfield
That listener could check the string length and if the length is greater than 7 you could clear any characters typed after the 7th.
That's just off the top of my head, there are likely other ways of handling it, too :)
Peach
I used that listener but How can i clear the character ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | --How to clear the text field here print( "TextField Object is: " .. tostring( getObj() ) ) if ( "began" == event.phase ) then -- This is the "keyboard has appeared" event elseif ( "ended" == event.phase ) then userName=tostring( getObj().text ); print(#userName) if #userName > 7 then --WHAT TO DO HERE ? it wont effect.. end print( "Text entered = " .. tostring( getObj().text ) ) -- display the text entered elseif ( "submitted" == event.phase ) then native.setKeyboardFocus( nil ) end |
Run this code on it's own for a demo;
1 2 3 4 5 6 7 8 9 10 11 | display.setStatusBar (display.HiddenStatusBar) local field limitField = function (event) if (string.len(field.text)) > 7 then field.text = field.text:sub(1,7) end end field = native.newTextField(10, 10, 100, 20, limitField) |
Thanks a lot for your time. I tried your code but after 7th character, it will add the character at the beginning of the text field..
Is this in the Xcode simulator or Corona simulator? Also, which version of Corona are you using?
I just testing using 704 and it works fine in both simulators.
Please let me know.
Peach :)
I'm using corona simulator 2011.704(2011.12.8) but tried it in android device becoz windows simulator don't show native text field.
vnshetty:)
Ah I see.
Unfortunately some textfield stuff will behave differently on Android; it would be worth posting in the Android sub forum to ask for a solution as another Android dev may have a workaround for you.
I'm afraid I deal almost exclusively with iOS and don't actually have an Android device here at the moment to test.
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Audio playback is grinding my app to a halt. [Hair-pulling stage]
How do you properly deal with spritesheet removal with director class?
Capturing the version in code
Transparent PNG Layers problem
Posting to Facebook App Affected?
Panda Drop
Hunters' Moon (iPad)
To make a REALLY user-friendly game you need a 90-year-old beta tester!
Alerts not working on Device