Harry
Harry

Reputation: 548

Flex spark TextArea limit max characters when copy/pasted

I did a search and was not able to find it. We have a Spark TextArea with maxChars="3900". But it doesn't work when copy/paste to the text area. I tried to add this to the changingHandler:

 if (ta.text.length > 3900)
        {
                        Alert.show("The maximum characters length is 3900. Please limit the characters to the max limit");
                        ta.text = ta.text.substr(0, 3900);
                    } else 
                    {
                        if (event.operation is PasteOperation)
                        {
....//Other logic
                        }
       }

The problem is it does not work all the time. The Alert shows up only some times when it is over 3900 chars. Not sure why. I also added the same to the changeHandler as well. But that does not get triggered at all.

Please let know what I'm missing. I need to show an alert & trim the chars to the max each time it goes above the max limit.

Thanks

Harish

Upvotes: 0

Views: 1805

Answers (1)

Guoliang
Guoliang

Reputation: 895

First, we need to clear one thing: when the changing handler is triggered, that's means: the text is changing, but the change not apply yet.

if the text in your textare is ""(empty), now, I paste 1600 chars, the changing handler invoked, the length of the text is still 0, because it's changing, not change.

so now, if you got a change handler, when you trace the length, it should be 1600.

but, if you use "event.preventDefault();" in your changing method, and do nothing to change the text in your changing handler, your change handler should not be triggered.

so, my suggestion is :

  1. Using changing handler is correct.
  2. in the handler, get the text in clipboard, then you can get the length of the text in clipboard, with this length + textArea.text.length, you'll get the length if the paste processed. if the total lenght is bigger than your limition, you can prevent the event, and do whatever you want to do.

here is some code for you:

protected function textArea_changingHandler(event:TextOperationEvent):void
        {
            trace(event.type + " - " + textArea.text.length); //  this length is befor the paste

            if(event.operation is PasteOperation) {
                // Text in the clipboard.
                var textPaste:String = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) == null ? "" : Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) as String; 
                // this length is after the paste(if the paste will complete)
                var totalLength:int = textArea.text.length + textPaste.length; 
                trace("String length after Paste: " + totalLength);

                if(totalLength > 3900) {
                    event.preventDefault();
                    textArea.text += "[Paste:" + textPaste.substr(0, 2) + "]"; // process your text here. 
                }
            }
        }

Upvotes: 2

Related Questions