07
Mar
08

Preventing line feeds in a TextArea control in Flex

The following example shows how you can prevent a user from pressing Enter in a Flex TextArea control by listening for the textInput event and checking the TextEvent object’s text property for a newline character (”\n”).

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/07/preventing-line-feeds-in-a-textarea-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function textArea_textInput(evt:TextEvent):void {
                if (evt.text == "\n") {
                    evt.preventDefault();
                }
            }
        ]]>
    </mx:Script>

    <mx:TextArea id="textArea"
            verticalScrollPolicy="on"
            width="160"
            height="120"
            textInput="textArea_textInput(event);">
        <mx:text>The quick brown fox jumped over the lazy dog.</mx:text>
    </mx:TextArea>

</mx:Application>

View source is enabled in the following example.


3 Responses to “Preventing line feeds in a TextArea control in Flex”


  1. 1 jexchan Mar 7th, 2008 at 10:42 pm

    Hi,Peter deHaan

    Thanks for your great post!

    I have a another question about this.

    I use TextArea component in flex, trigger “Ctrl + Enter” event to newline(use this to send messege in my project), but more empty newline in IE(it’s ok in Firefox or trigger ‘Shift+Enter’),how can i solve this problem?

    Thank you very much!!!

    Code like below

    if(evt.ctrlKey == true && evt.keyCode == Keyboard.ENTER) {
    ……
    }

  2. 2 manish sankhe Mar 20th, 2008 at 11:12 pm

    hi dis is manish workin in teknopoint multimedia thanks for givin the source of this code…i was trying to disable the enter key by using ifocus manager bt everythin invain..ur code help me out in dis thanks….

  3. 3 Learning Everthing Apr 24th, 2008 at 11:55 pm

    How can I disable backspace, left arrow and right arrow keys ?

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".