Skip to main content
April 20, 2006
Answered

input text - limit characters

  • April 20, 2006
  • 4 replies
  • 509 views
Hi. I have a input textfield that is let say 100 px width. Is there any way to limit the number of character so that they fit in the field. I don't want to use maxChars since i want to use the whole field end diffrent characters has diffrent width.

Hope you understand what I mean.

/Antewik
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer blemmo
You can use the TextFormat.getTextExtent() method to get the width of a given string for a certain textformat. Compare this with the textfield width and disable input if it reaches the limit.

cheers,
blemmo

4 replies

Inspiring
April 20, 2006
googling would help. or reading the macromedia livedocs:

http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001840.html#wp4009378

RTFM
blemmoCorrect answer
Inspiring
April 20, 2006
You can use the TextFormat.getTextExtent() method to get the width of a given string for a certain textformat. Compare this with the textfield width and disable input if it reaches the limit.

cheers,
blemmo
April 20, 2006
I was just trying something like that. I just don't now exactly how I write that code.
Inspiring
April 20, 2006
I made a quick example:
--
// 'input' is the textfield's name
var tf:TextFormat = input.getNewTextFormat();
var lasttext:String;
input.onChanged = function() {
if(tf.getTextExtent(input.text).textFieldWidth > input._width-10){
trace("full");
input.text = lasttext;
}
else {
lasttext = input.text;
}
};
--
Maybe it won't work on some Mac players, according to some comments on the LiveDocs site. There are similar codes on that site, I don't know if they may be better, just had a quick look at it.

hth,
blemmo