Skip to main content
April 9, 2009
Question

How to limit user input in text box.

  • April 9, 2009
  • 1 reply
  • 1016 views

I have a problem with limitation of user input. I have a container linked with text flow and I have to don't allow user to type a lot of text. User has to be able to type text while text is not scrolled. User has to see all text always. I've tried to find something useful in documentation but I've not found anything. Does somebody know how to implement this limitation?

This topic has been closed for replies.

1 reply

Adobe Employee
April 10, 2009

You can add an event listener to the textFlow for the FlowOperationEvent.FLOW_OPERATION_END. Your listener will get called for every text editing operation, after its been completed. If it doesn't like what got inserted, it can remove it by doing a delete. Here's some example code that does this for PasteOperations:

textFlow.addEventListener(FlowOperationEvent.FLOW_OPERATION_END,operationEndHandler,false,0,true);

private function operationEndHandler(event:FlowOperationEvent):void

{

     var selectionManager:ISelectionManager =  textFlow.interactionManager;

     var operation:PasteOperation = event.operation as PasteOperation
     if (operation && SelManager.textFlow.textLength > maxFlowLength)
      {
          var trimAmt:int = textFlow.textLength - maxFlowLength;
          var pasteEnd:int = pastePosition + (operation.absoluteEnd - operation.absoluteStart);
          selectionManager.setSelection(pasteEnd - trimAmt, pasteEnd);
          (selectionManager as IEditManager).deleteNext();
       }

If it was only concerned about insertText, the code could have listened for FLOW_OPERATION_BEGIN, and cancelled operations that exceeeded the limit. But if you allow paste, there is no way to know in advance what is going to be pasted, so this works out to be the best method.

Hope this helps,

- robin

April 10, 2009

Thanks for answer but problem is another, I can limit the text length but I don't know what is the limit. User can add some lines with 1-3 symbols or some lines with 20-30 symbols. Length is different but text box can store them if it has adequate size. And user can type 10 lines with 1 symbol per line. It seems I have to look on line count in text box... Thanks for answer in any case, I didn't look on FLOW_OPERATION_END and it's useful event.

I've done it with checking of line position. It works not very well but it works

        private function checkSizeLimit(event:FlowOperationEvent):void
        {
            var editManager:EditManager =  EditManager(TFlow.interactionManager);
            var lastLine:TextFlowLine = TFlow.flowComposer.getLineAt(TFlow.flowComposer.numLines-1);    //get last line of text in our textbox
            if((lastLine.y+lastLine.height) > controller.compositionHeight)//check is line in textbox or not
            {//undo operation if part of line is not in textbox
                event.operation.undo();
            }
        }