Copy link to clipboard
Copied
Hello,
I need to count how many times a letter is contained inside a string as the user types in the textbox (i.e., in real time) and display the count on the page? I would really appreciate it if someone can give me some guidance how to do that in ActionScript 3.0/Flash CS6.
Thanks a lot in advance!
One element you will need to use is a listener for the CHANGE Event of a textfield. You will use that as a trigger to check the text in the textfield.
The details for how you check the text can depend partly on how the text is allowed to be entered, but you essentially need to examine each character that is entered and determine if it is the character you are counting, and if so, add it to t a sum. You can use the charAt() method of the String class to get individual characters.
Copy link to clipboard
Copied
One element you will need to use is a listener for the CHANGE Event of a textfield. You will use that as a trigger to check the text in the textfield.
The details for how you check the text can depend partly on how the text is allowed to be entered, but you essentially need to examine each character that is entered and determine if it is the character you are counting, and if so, add it to t a sum. You can use the charAt() method of the String class to get individual characters.
Copy link to clipboard
Copied
Thanks a lot for your reply! That is really helpful!
Is that right?
stop();
this.onEnterFrame = function() {
myletter = "e";
mytext = formtext.text;
formtext.addEventListener(TextEvent.TEXT_INPUT, checkfunction);
function checkfunction(insTxtEv.text:TextEvent):void
{
function getEntranceNumber(mytext:String,myletter:String):Number
{
if( myletter.length>1)
{
trace("length of a letter should be equal to 1");
return 0;
}
else
{
var total:Number = 0;
var i:Number;
for( i=0 ; i<mytext.length ; i++ )
{
if( mytext==myletter[0] )
total++;
}
return total;
outputtext.htmlText = total;
}
}
}
}
btnContinue10.onRelease = function() {
storedata("text",inputtext);
nextFrame();
}
Copy link to clipboard
Copied
At first glance and then a couple others I have to say no.
You appear to be mixing AS2 and AS3 into your code and that will not work at all.
You appear to be nesting one function inside another - something you should avoid at all costs.
I said to use a CHANGE Event for a reason... a TEXT_INPUT event is triggered before the text is actually entered, so if you happen to be checking the text in the textfield you will be missing the last character that was entered. Use Event.CHANGE instead of TextEvent.TEXT_INPUT
You should have no use for an enterframe function unless you need iot for something else. The CHANGE listener is sufficient as it will be triggered any time the keyboard is used to change the text.
Copy link to clipboard
Copied
The code below illustrates how it can be done. It draws two TextFields, reacts to input and writes number of times letter was inputted.
Just paste it on a timeline in a new FLA
var inputText:TextField;
var feedbackText:TextField;
var myletter:String = "e";
init();
function init():void
{
inputText = new TextField();
inputText.defaultTextFormat = new TextFormat("Arial", 12);
inputText.type = TextFieldType.INPUT;
inputText.multiline = inputText.wordWrap = false;
inputText.border = true;
inputText.width = 200;
inputText.height = 22;
inputText.x = inputText.y = 20;
feedbackText = new TextField();
feedbackText.defaultTextFormat = new TextFormat("Arial", 12, 0xff0000);
feedbackText.multiline = feedbackText.wordWrap = false;
feedbackText.width = 200;
feedbackText.height = 22;
feedbackText.text = "letter " + myletter + " was entered 0 times";
feedbackText.x = inputText.x;
feedbackText.y = inputText.y + inputText.height + 10;
addChild(inputText);
addChild(feedbackText);
inputText.addEventListener(Event.CHANGE, onInput);
}
function onInput(e:Event):void
{
feedbackText.text = "letter " + myletter + " was entered " + inputText.text.match(new RegExp(myletter, "gi")).length + " times";
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now