Restricting text entry to a certain number of lines in Actionscript

This one deviates a bit from my usual video workflow stuff, but I’ve been writing a lot of Actionscript and spent too much time researching this topic without a lot of great answers.

It’s surprisingly hard to restrict text entry to a limited number of lines in an input text field in Flash. You can restrict characters, but unless you’re using a monospaced font it can produce inconsistent results and you have to make sure you catch the return and enter keys so that you don’t scroll into oblivion. The following code simply replaces the return and enter key with a space and uses an onChange handler to make sure we don’t exceed maxscroll, which in this case is hard coded at 3 lines. It works pretty well and handles some edge cases like copy/pasting by restricting the paste to just visible characters in the text field.

textEntry.onChanged = function(){
     var symbol:String = "\r";
     var symbolPos:Number = textEntry.text.indexOf(symbol);

     if (symbolPos > -1) {
		textEntry.replaceText(symbolPos, symbolPos + 1, ' ');
	} else {
		//trace("symbol '"+symbol+"' not found.");
	for(i=0; i< textEntry.text.length;  i++){
		textEntry.scroll = textEntry.maxscroll;

		if(textEntry.bottomScroll > 3){
			  textEntry.text = textEntry.text.slice(0, textEntry.text.length-1);


Comments are closed.