Using the Event.CHANGE event listener, you can get notified every time the contents of a text field changes (ie. the user typed or deleted characters)
Then you'll have to use String.match() to find the number of occurrences. But to count them all, you have to pass a Regular Expression to match(). Regular Expression are basically string voodoo which are fairly difficult to understand but can do amazing things. In your case a simple character match is not too difficult.Code:myTextField.addEventListener(Event.CHANGE, onTextChange);
Code:function onTextChange(event:Event):void{ // here you can do the counting // first we create the expression to match. // The single forward slashes '/' mean "this is the beginning and end of my expression". // The 'g' on the end is a modifier that tells it we want a global match meaning "find all the matches in this string" // the open and close brackets '[]' means "here is a list of things to look for". In this example we are searching or any lower case or upper case 'S' // the pipe between the two s's means match a lower case OR an upper case. var pattern:RegExp = /[s|S]/g; // now we call match() on the users input and pass in the RegExp. // it returns an array of all matches so the length of that array is your number. var results:Array = myTextField.text.match(pattern); trace ("There are:", results.length, "S's in the text field"); // If the users in put was "See Spot Run" this would trace: "There are 2 S's in the text field" }




Reply With Quote