Project

General

Profile

How to limit input length of WTextArea

Added by Steve Drake about 11 years ago

I would like to limit the number of characters that the user can enter into a WTextArea, similar to using WLineEdit::setMaxLength(int length).

How could I do this? (I am using Wt 3.2.1). I tried creating a WLengthValidator and passing it to WTextArea::setValidator(WValidator *validator). The border around the WTextArea turns red when too much text is entered, but it does not prevent the user from continuing to enter more text.


Replies (2)

RE: How to limit input length of WTextArea - Added by Koen Deforche about 11 years ago

Hey,

A validator cannot prevent such a thing (only flag it).

It does seem that textarea has native support for this (on most browsers but only on IE10+). If that's acceptible you can use setAttributeValue() to set the "maxlength" attribute.

Btw a user can always work around this so you should really only trust server-side validation.

Regards,

koen

RE: How to limit input length of WTextArea - Added by Steve Drake about 11 years ago

The setAttributeValue() approach worked for me, where supported by the browser. (It is not supported by IE 9 or Opera 12.16 but there is not much I can do about that.)

By the way, I briefly thought about using setAttributeValue() before you suggested it, but I was discouraged by the following statement in the documentation: "This is only useful when processing dom nodes associated with widgets in custom JavaScript code".

Thanks,

Steve

    (1-2/2)