Solved Limit the number of input characters in TextArea
-
Hi,
How to limit the number of input characters in TextArea?
there is one feature in TextInput name maximumLength for that how about TextArea?Thanks,
-
There is nothing built-in, but you can implement the limit with something like this:
TextArea { onTextChanged: if (length > MyLimit) remove(MyLimit, length); }
-
Dear @sierdzio ,
Thanks it works nicely. -
:-) you're welcome, happy coding!
-
Indeed, I support using this way:
onTextChanged: {
remove((cursorPosition-(length-maxlen)),cursorPosition)
}
it can prevent user from inserting characters in the middle part of the text.
Howerer, when "ctrl + z" is typed, there're still problems. -
I think this is better:
property int maximumLength: -1; property string lastValidText: ""; onTextChanged: { if (text.length > maximumLength && maximumLength>-1) { var currentPosition = cursorPosition-1; text = lastValidText; cursorPosition = currentPosition; } else { lastValidText = text; } }