Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-14-2006, 09:57 AM   #1
synergie7
Registered User
 
Join Date: Jul 2005
Posts: 1
Default How do I limit input into a TextField - not using maxChars

I would like to limit the amount of text that a user can type in to a TextField, which is 2 lines high, so that they can't type past the end of the 2nd line.

I can't use maxChars as the actual position of the last char they type will depend on which characters they have typed in, e.g. WWWWWW is wider than iiiiii.

I've tried attaching a listener to the onChanged event, and then use the value of "scroll" to determine when the TextField wants to scroll, and removing the last char that was typed if the TextField as scrolled.
BUT, onChanged doesn't get called if the user inputs spaces. So if they type a load of spaces at the end of the 2nd line, it will wrap around and onChanged won't pick up that this has happened until they type a non whitespace char.

In the mean time, the I-beam has vanished. This is not ideal.

So far the only workable solution seems to be to have a hidden TextField of the same size as the on screen one, and use the Key.onKeyUp event to monitor what gets typed in the visible TextField. Set the text of the hidden TextField to the text of the visible TextField + a "W" on the end, and watch the scroll or maxscroll properties of the hidden TextField to alert me when the visible TextField would have wrapped around, and remove the offending char.

Does anyone have a better solution
synergie7 is offline   Reply With Quote
Old 09-28-2006, 08:57 PM   #2
Paradise
Registered User
 
Join Date: Sep 2006
Posts: 14
Default

i'm bumping this because i had trouble grasping what synergie7 said. i'm just going to post the code and then people can analyze it if they want or just grab it.

i don't have a better way but I do have a way to handle the copying and pasting multiple characters scenario as well.

Code:
myText.depthChild0._alpha = 50;
myText.hScrollPolicy = "off";
myTextDummy.hScrollPolicy = "off";
myText.vScrollPolicy = "off";
myTextDummy.vScrollPolicy = "off";

var listenerObject:Object = new Object();
listenerObject.change = function(eventObject:Object) {
	var myTextDummyLength:Number = myText.length - myTextDummy.length; //handle cut and paste multiple characters overflow scenario
	
	myTextDummy.text = myText.text;
	
	if (myTextDummy.maxVPosition > 0) {
		myText.text = myText.text.substring(0, myText.length - myTextDummyLength);
		myTextDummy.text = myText.text;
	}
};
myText.addEventListener("change", listenerObject);
Paradise is offline   Reply With Quote
Old 02-13-2007, 09:26 PM   #3
Freddie Fisher Oxford
Registered User
 
Join Date: Feb 2007
Posts: 3
Default A neat solution for limiting the number of lines in flash

OK finally found a good way of limiting the number of lines. This will work even if you try to paste in a large amount of characters at once. In this instance i didn't want the text to expand such that there was a maxscroll of 15 lines, thus limiting my text to 20 lines (textShortWrite is tall enough to hold 5 lines on the screen at once).

apologies for the non standard formatting, I prefer to code in a 'tab hierarchy of conditions' style and I personally find it a lot easier to read than conventional actionscript style.


NB It is a good idea to have the condition that changes the textfield type first because otherwise the app can crash if the user pressess ctr + V very quickly with large chunks of characters on the clipboard. This way, the swf file will ignore all of these until it has already dealt with the first offending one.

Code:
this.textShortWrite.onChanged = function()


	{
		
	
	if (textShortWrite.maxscroll  >= 15) //if the max number of lines is exceeded
	
		{
		
		//stop the user from any further editing for the time being:
		
		textShortWrite.type = "dynamic";

			
		}
	
	
	
		
	while (textShortWrite.maxscroll  >= 15) //starts if maxscroll >= 15
	
		{
		
		current_string = textShortWrite.text; 
		
		textShortWrite.text = current_string.slice(0, -1); //slice off the last character of current string
		
		
		}//end of while loop. loop stops only when the maxscroll is 14 or less.

				
	
	textShortWrite.type = "input"; //turn the box back to an input box, if it was changed
	
	
		
	
	} //end of onChanged event handler
Freddie Fisher Oxford is offline   Reply With Quote
Old 09-02-2009, 05:56 PM   #4
opensea
MMP in Ankara
 
Join Date: Oct 2006
Posts: 237
Send a message via ICQ to opensea Send a message via MSN to opensea
Default

Quote:
Originally Posted by Freddie Fisher Oxford View Post
OK finally found a good way of limiting the number of lines. This will work even if you try to paste in a large amount of characters at once. In this instance i didn't want the text to expand such that there was a maxscroll of 15 lines, thus limiting my text to 20 lines (textShortWrite is tall enough to hold 5 lines on the screen at once).

apologies for the non standard formatting, I prefer to code in a 'tab hierarchy of conditions' style and I personally find it a lot easier to read than conventional actionscript style.


NB It is a good idea to have the condition that changes the textfield type first because otherwise the app can crash if the user pressess ctr + V very quickly with large chunks of characters on the clipboard. This way, the swf file will ignore all of these until it has already dealt with the first offending one.

Code:
this.textShortWrite.onChanged = function()


	{
		
	
	if (textShortWrite.maxscroll  >= 15) //if the max number of lines is exceeded
	
		{
		
		//stop the user from any further editing for the time being:
		
		textShortWrite.type = "dynamic";

			
		}
	
	
	
		
	while (textShortWrite.maxscroll  >= 15) //starts if maxscroll >= 15
	
		{
		
		current_string = textShortWrite.text; 
		
		textShortWrite.text = current_string.slice(0, -1); //slice off the last character of current string
		
		
		}//end of while loop. loop stops only when the maxscroll is 14 or less.

				
	
	textShortWrite.type = "input"; //turn the box back to an input box, if it was changed
	
	
		
	
	} //end of onChanged event handler
wow. your code saved my life.
thanks
opensea is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
input textfield limit lines martijneerens ActionScript 2.0 7 01-16-2012 04:18 PM
embedFont stops runtime generated input textField working grandy ActionScript 1.0 (and below) 2 02-02-2008 11:11 PM
Lost focus on Input textField PINGELS ActionScript 1.0 (and below) 3 04-29-2005 08:42 PM
dynamically font-embedded input textfield problem PsychoSpike ActionScript 2.0 6 03-16-2005 10:29 PM
Input Textfield with Stylesheet = problem; simonboris ActionScript 2.0 2 12-03-2003 07:04 AM


All times are GMT. The time now is 11:28 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.