Restrict TextField to N Words instead of N Characters


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package {
  2. import flash.display.*;
  3. import flash.events.*;
  4. import flash.text.*;
  5.  
  6. public class Main extends Sprite {
  7. private var _inputfield:TextField;
  8.  
  9. public function Main() {
  10.  
  11. _inputfield = new TextField( );
  12. _inputfield.addEventListener(Event.CHANGE, changeListener);
  13. _inputfield.width = 150;
  14. _inputfield.height = 30;
  15. _inputfield.border = true;
  16. _inputfield.background = true;
  17. _inputfield.type = TextFieldType.INPUT;
  18. addChild(_inputfield);
  19.  
  20.  
  21. }
  22. private function changeListener (e:Event):void {
  23. var formTextField : TextField = TextField( evtObj.target );
  24. var wordsArr:Array = formTextField.text.split(" ");
  25.  
  26. if(wordsArr.length == MAX_WORDS) {
  27. formTextField.maxChars = formTextField.text.length;
  28. }
  29. }
  30. }
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.