TextField / TextBox

Is it possible to restrict the amount of characters the user is able to type?

I only want them to be able to type 30 characters at most. [import]uid: 8697 topic_id: 31228 reply_id: 331228[/import]

You can during the editing phase of the textfield handler test the length of the string and if it’s too long, chop it at your desired length. [import]uid: 19626 topic_id: 31228 reply_id: 124908[/import]

During the “editing” event.phase you can check to see the length of the string and when it hits the limit+1 chop off the last character.

In the docs they say event.oldText shows what was there before the current character was typed, but that seems to be missing in real-world use. If they get that fixed/back in, no chopping would be needed, just replace the text with event.oldText when the max length was reached.

Jay
[import]uid: 9440 topic_id: 31228 reply_id: 124937[/import]

You can during the editing phase of the textfield handler test the length of the string and if it’s too long, chop it at your desired length. [import]uid: 19626 topic_id: 31228 reply_id: 124908[/import]

During the “editing” event.phase you can check to see the length of the string and when it hits the limit+1 chop off the last character.

In the docs they say event.oldText shows what was there before the current character was typed, but that seems to be missing in real-world use. If they get that fixed/back in, no chopping would be needed, just replace the text with event.oldText when the max length was reached.

Jay
[import]uid: 9440 topic_id: 31228 reply_id: 124937[/import]

I also had same problem and using of event.oldText is works fine, thanks for the idea. But there is a some new problem: after “textBox.text = event.oldText” command,
the cursor is set to startPosition =1 in the textBox (the beginning of the text in the textBox) . It is expected that the cursor will remain at the end of the text.
How can I fix it? [import]uid: 172733 topic_id: 31228 reply_id: 126105[/import]

Wow, that looks like a bad problem because I don’t see anywhere in the docs where you can set the cursor position in a TextField.

But, the docs seem to be spread over hundreds of acres, so it’s possible I’ve missed it.

Maybe someone from CL will stop by and give the definitive word…

Jay
[import]uid: 9440 topic_id: 31228 reply_id: 126161[/import]

The following works for me on iOS devices.

if ( “editing” == event.phase ) then
if string.len(lastNameFieldNative.text) > 30 then
lastNameFieldNative.text = lastNameFieldNative.text:sub(1,30)
end
end
[import]uid: 48484 topic_id: 31228 reply_id: 126183[/import]

I also had same problem and using of event.oldText is works fine, thanks for the idea. But there is a some new problem: after “textBox.text = event.oldText” command,
the cursor is set to startPosition =1 in the textBox (the beginning of the text in the textBox) . It is expected that the cursor will remain at the end of the text.
How can I fix it? [import]uid: 172733 topic_id: 31228 reply_id: 126105[/import]

Wow, that looks like a bad problem because I don’t see anywhere in the docs where you can set the cursor position in a TextField.

But, the docs seem to be spread over hundreds of acres, so it’s possible I’ve missed it.

Maybe someone from CL will stop by and give the definitive word…

Jay
[import]uid: 9440 topic_id: 31228 reply_id: 126161[/import]

The following works for me on iOS devices.

if ( “editing” == event.phase ) then
if string.len(lastNameFieldNative.text) > 30 then
lastNameFieldNative.text = lastNameFieldNative.text:sub(1,30)
end
end
[import]uid: 48484 topic_id: 31228 reply_id: 126183[/import]