How to restrict number of character in Native text field

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?

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.

views:2943 update:2012/2/6 12:03:31
corona forums © 2003-2011