Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 111
Registered: ‎07-05-2012
My Device: none
My Carrier: MOvistar
Accepted Solution

BB10: How To Limit Number of Characters in a TextField

Hi guys,

 

I need to set a max length for a TextField.

 

It seems there's no any property that does this.

 

Any idea about this?

 

Thanks!

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: BB10: How To Limit Number of Characters in a TextField

[ Edited ]

The first approach which comes to mind is trimming the string to required length in onTextChanging handler.
Something like
if (id.text.length > 30) id.text = id.text.substr(0,30);


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: BB10: How To Limit Number of Characters in a TextField

But for some kind of languages, because one character has two bytes, it also has the possibility that the result is over the expect length of bytes. We should be aware of this case.




p(^_^)q
Good good study, day day up
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: BB10: How To Limit Number of Characters in a TextField

AFAIK length method in JavaScript returns the number of characters, not the length in bytes.

Internally it stores the characters as UCS-2 or UTF-16.

Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: BB10: How To Limit Number of Characters in a TextField

[ Edited ]

Yes. It is what I worry about. Such as there is one length restrain in one DB field say 20 bytes, if one text has 20 characters but it maybe has 40 bytes. Although it can pass the length check for characters at the client side,  when it is inserted into that DB field, it will cause an error.

 

I just extend this topic. lol




p(^_^)q
Good good study, day day up
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: BB10: How To Limit Number of Characters in a TextField

Ah, I see now. Good point!

Andrey Fidrya, @zmeyc on twitter
Highlighted
Developer
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: BB10: How To Limit Number of Characters in a TextField

Thank you.




p(^_^)q
Good good study, day day up
Regular Contributor
Posts: 76
Registered: ‎06-14-2013
My Device: Z10
My Carrier: PosteMobile

Re: BB10: How To Limit Number of Characters in a TextField

[ Edited ]

You can easily set the maximum TextField length by the method :

Q_SLOT void setMaximumLength(int maximumLength)

available from API 10.2