View Full Version : how to restrict the max. no of chars of a textfield?
Martin.Trummer
20 Mar 2009, 7:23 AM
how can I set the HTML-input attribute maxLength on gxt textfields?
the textfield has a setMaxLength() method, but this won't set the html-attribute maxLength of the input field - that means: the user can enter long text, and when the text is longer than specified in maxLength a validation error will occur, telling the user, that the text is too long.
I'd prefer to restrict the number of input-characters to maxLength in the first place, so that the user cannot enter a text that is too long.
fother
20 Mar 2009, 7:57 AM
implements setValidator
Martin.Trummer
23 Mar 2009, 4:03 AM
implements setValidator
That does not make any sense to me.
Could you please explain in more detail what you mean?
fother
3 Apr 2009, 3:50 AM
search in forum... have many examples ;)
Martin.Trummer
3 Apr 2009, 6:44 AM
Did you actually understand my problem?
I don't think implementing my own validator will help:
1) there's no need to implement a length validator for text fields - this is already done per default in TextFiled.validateValue() when you set have set the maxLength of the field
2) even if I did write my own validator and set it to the text field, it would have the same behaviour as described before: it would show an error to the user, AFTER she had entered too many chars: Bbut what I want is to set the html-attribute maxLength, so that there is no way of entering too many chars in the first place.
For example: http://de.selfhtml.org/html/formulare/anzeige/input_text.htm has 2 input fields which are both restricted to 30 chars and there's no way of entering the 31st char:
That's the way it should be: prevent errors, instead of shouting at users that they did something wrong.
fother
3 Apr 2009, 7:50 AM
hmm.. I think that I understand..
public void onModuleLoad() {
TextField<String> field = new TextField<String>() {
@Override
protected void onKeyPress(FieldEvent fe) {
super.onKeyPress(fe);
if (getRawValue() != null && getRawValue().length() == getMaxLength()) {
fe.stopEvent();
}
}
};
field.setMaxLength(5);
RootPanel.get().add(field);
}
mjcee
3 Apr 2009, 11:28 AM
See this link for another method.
http://extjs.com/forum/showthread.php?t=50418&highlight=maxLength
Martin.Trummer
6 Apr 2009, 12:47 AM
See this link for another method.
http://extjs.com/forum/showthread.php?t=50418&highlight=maxLength
thanks, that looks good.
but that's still a little inconvenient to GXT-users.
we have to extend textfield and use our own TextField-Classes everywhere.
I hope GXT-fields will provide a way to directly set this attribute in future versions
fother
7 Apr 2009, 11:19 AM
+1
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.