native.*

I am going mad trying to sort some basic problems with native.newTextbox.

All I want to do is let the user type in a paragraph of text and then

a) check the length of the text against a maximum value whilst they are typing and stop them when they reach the limit.

b) catch them typing return and delete the return (ie don’t let them enter a newline in the text (the text wraps ok without).

Ive tried many many suggestions from the user forum and none of them actually work. In essence they all suggest that in the phase = “editing” there are several properties available that can be manipulated:

event.newCharacters, event.oldText, event.text to be specific. What I find is that these are all as described but do not actually affect the contents of the inputbox, for example if I limit the text length by checking the length of event.text and when it exceeds my maximum length I set event.text = event.oldText (even if using intermediate variables, setting event.text to anything doesn’t affect the actual text in the box.

With regard to the newline character, the event.newCharacter doesn’t show it. when you type a backspace or a return in a textbox event.newCharacter is empty.

Am I missing something here? I

I realise you can write a whole lotta games without any text entry, but there must be a way of validating the text on entry.

I’m on my phone, but instead of event.text = oldText do textBoxObject.text = oldText to affect text in box.

event.text is a copy of what is in the text field, it is not the text field’s text property.  You can modify event.target.text to actually work with the contents of the text field.

Rob

Thanks Rob, that solved it. Interestingly the comment from jonjonsson should have worked too but didn’t.

Any ideas about the other issue? How do I capture a return in the multiline box? I know I can do it after they have submitted but it looks messy.

Rob, could you see that the documentation on native.textBox is changed to show your point, major improvement just by pointing out the obvious that event.text is a copy.

Solved. Rob’s first answer allowed the solution to the second problem too.

The userinput event doesn’t capture backspace but then you want to allow users to correct mistakes. It does capture other control keys and in particular in my case return (char 10). The following code is how I then carry out the test on input text to remove return key presses and to limit text. the textlimit could be a variable but I don’t need that. Hope this helps others.

Function onInputName(event)

     local phase = event.phase

     if “began”== phase then

    elseif “ended” == phase then

    elseif “submitted” == phase then

    elseif “editing” == phase then

        local txt = event.text

        local newChar = event.newCharacters

        local strlen = string.len(txt)

        if strlen >20 then – testing for length exceeding desired limit

            txt= string.sub(txt,1,20)

            event.target.text = txt

         elseif string.byte(event.newCharacters) == 10 then – testing for return key entered

             if strlen == 1 then

                 txt = “”

             else

                 txt = string.sub(txt,1,strlen-1)

             end

             event.target.text = txt

         end

    end

    return true

end

If you want shorter text you can just use event.text directly obviously.

Function onInputName(event)

     local phase = event.phase

     if “began”== phase then

    elseif “ended” == phase then

    elseif “submitted” == phase then

    elseif “editing” == phase then

        if string.len(event.text) >20 then – testing for length exceeding desired limit (20 in this case)

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

        elseif string.byte(event.newCharacters) == 10 then – testing for return key entered

             if string.sub (event.text)== 1 then

                 event.target.text = “”

             else

                 event.target.text = string.sub(txt,1,strlen-1)

             end

         end

    end

    return true

end

I thought we were updating the docs.  We may just have not gotten to it yet. 

Rob

I’m on my phone, but instead of event.text = oldText do textBoxObject.text = oldText to affect text in box.

event.text is a copy of what is in the text field, it is not the text field’s text property.  You can modify event.target.text to actually work with the contents of the text field.

Rob

Thanks Rob, that solved it. Interestingly the comment from jonjonsson should have worked too but didn’t.

Any ideas about the other issue? How do I capture a return in the multiline box? I know I can do it after they have submitted but it looks messy.

Rob, could you see that the documentation on native.textBox is changed to show your point, major improvement just by pointing out the obvious that event.text is a copy.

Solved. Rob’s first answer allowed the solution to the second problem too.

The userinput event doesn’t capture backspace but then you want to allow users to correct mistakes. It does capture other control keys and in particular in my case return (char 10). The following code is how I then carry out the test on input text to remove return key presses and to limit text. the textlimit could be a variable but I don’t need that. Hope this helps others.

Function onInputName(event)

     local phase = event.phase

     if “began”== phase then

    elseif “ended” == phase then

    elseif “submitted” == phase then

    elseif “editing” == phase then

        local txt = event.text

        local newChar = event.newCharacters

        local strlen = string.len(txt)

        if strlen >20 then – testing for length exceeding desired limit

            txt= string.sub(txt,1,20)

            event.target.text = txt

         elseif string.byte(event.newCharacters) == 10 then – testing for return key entered

             if strlen == 1 then

                 txt = “”

             else

                 txt = string.sub(txt,1,strlen-1)

             end

             event.target.text = txt

         end

    end

    return true

end

If you want shorter text you can just use event.text directly obviously.

Function onInputName(event)

     local phase = event.phase

     if “began”== phase then

    elseif “ended” == phase then

    elseif “submitted” == phase then

    elseif “editing” == phase then

        if string.len(event.text) >20 then – testing for length exceeding desired limit (20 in this case)

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

        elseif string.byte(event.newCharacters) == 10 then – testing for return key entered

             if string.sub (event.text)== 1 then

                 event.target.text = “”

             else

                 event.target.text = string.sub(txt,1,strlen-1)

             end

         end

    end

    return true

end

I thought we were updating the docs.  We may just have not gotten to it yet. 

Rob