textfield LIMIT any help..

how can i limit my textfield…?

i want 10letters only…

tnx advance…

up

In your call back listener function you have to make sure the text doesn’t grow more than 10 characters.

if event.phase == “editing” then

     if string.len(event.target.text) > 0 then

          event.target.text = string.sub(event.target.text, 1, 10)

    end

end

or something like that.

something like this sir?  

  if ( “submitted” == event.phase ) then

        native.setKeyboardFocus( nil )

    end

if event.phase == “editing” then

     if string.len(event.target.text) > 0 then

          event.target.text = string.sub(event.target.text, 1, 10)

    end

end

Something like that.

Rob

but i try dat sir… its not working…

this is my code:


    local function onUsername( event )

    if ( “began” == event.phase ) then

    end

end

    if ( “submitted” == event.phase ) then

        native.setKeyboardFocus( nil )

    end

        if event.phase == “editing” then

            local txtlimit = event.text

            if(string.len(txtlimit)>9)then

                txtlimit=string.sub(txtlimit, 1, 9)

                event.text=txtlimit

            end

            end

usernameField = native.newTextField( 155, 150, 110, 35, onUsername )

usernameField.font = native.newFont( “PTSans-Regular”, 17)

usernameField.text = “Student”

usernameField:setTextColor( 255,0, 255 )

usernameField.align = “center”

usernameField:setReturnKey(“done”)


Don’t change event.text, change event.target.text

not working ser this is my code:

usernameField = native.newTextField( 155, 150, 110, 35, onUsername )

usernameField.font = native.newFont( “PTSans-Regular”, 17)

usernameField.text = “Student”

    local function onUsername( event )

    if ( “began” == event.phase ) then

    end

end

    if ( “submitted” == event.phase ) then

        native.setKeyboardFocus( nil )

   end

          if event.phase == “editing” then

          if string.len(event.target.text) > 0 then

          event.target.text = string.sub(event.target.text, 1, 10)

    end

    end

Is the onUsername() function defined after this line:

usernameField = native.newTextField( 155, 150, 110, 35, onUsername )

or before it?  If your code is in the order above, you have a scoping problem.  You try to use “onUsername” before you define it and it’s nil at that point so your function never gets run.  Move the function above the call to native.newTextField()

To learn more about scope and programming, see these resources:

http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

http://www.coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/

https://www.youtube.com/watch?v=j4U0ii1FjwE

https://www.youtube.com/watch?v=2ATlcGP2zMY

Rob

up

In your call back listener function you have to make sure the text doesn’t grow more than 10 characters.

if event.phase == “editing” then

     if string.len(event.target.text) > 0 then

          event.target.text = string.sub(event.target.text, 1, 10)

    end

end

or something like that.

something like this sir?  

  if ( “submitted” == event.phase ) then

        native.setKeyboardFocus( nil )

    end

if event.phase == “editing” then

     if string.len(event.target.text) > 0 then

          event.target.text = string.sub(event.target.text, 1, 10)

    end

end

Something like that.

Rob

but i try dat sir… its not working…

this is my code:


    local function onUsername( event )

    if ( “began” == event.phase ) then

    end

end

    if ( “submitted” == event.phase ) then

        native.setKeyboardFocus( nil )

    end

        if event.phase == “editing” then

            local txtlimit = event.text

            if(string.len(txtlimit)>9)then

                txtlimit=string.sub(txtlimit, 1, 9)

                event.text=txtlimit

            end

            end

usernameField = native.newTextField( 155, 150, 110, 35, onUsername )

usernameField.font = native.newFont( “PTSans-Regular”, 17)

usernameField.text = “Student”

usernameField:setTextColor( 255,0, 255 )

usernameField.align = “center”

usernameField:setReturnKey(“done”)


Don’t change event.text, change event.target.text

not working ser this is my code:

usernameField = native.newTextField( 155, 150, 110, 35, onUsername )

usernameField.font = native.newFont( “PTSans-Regular”, 17)

usernameField.text = “Student”

    local function onUsername( event )

    if ( “began” == event.phase ) then

    end

end

    if ( “submitted” == event.phase ) then

        native.setKeyboardFocus( nil )

   end

          if event.phase == “editing” then

          if string.len(event.target.text) > 0 then

          event.target.text = string.sub(event.target.text, 1, 10)

    end

    end

Is the onUsername() function defined after this line:

usernameField = native.newTextField( 155, 150, 110, 35, onUsername )

or before it?  If your code is in the order above, you have a scoping problem.  You try to use “onUsername” before you define it and it’s nil at that point so your function never gets run.  Move the function above the call to native.newTextField()

To learn more about scope and programming, see these resources:

http://omnigeek.robmiracle.com/2011/10/14/understanding-scope-for-beginning-programmers/

http://www.coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/

https://www.youtube.com/watch?v=j4U0ii1FjwE

https://www.youtube.com/watch?v=2ATlcGP2zMY

Rob