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.

Update 8/19/2008: If you want to prevent a user from pasting text with a carriage return/linefeed, you could listen for the change event and use the String class’s replace() method to replace any newline (”\n”) or linefeed (”\r”) with an empty string, as seen in the following example:

<?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();
                }
            }
            private function textArea_change(evt:Event):void {
                textArea.text = textArea.text.replace(/\n|\r/ig, "");
            }
        ]]>
    </mx:Script>

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

</mx:Application>

10 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 ?

  4. 4 çin, cin Jul 29th, 2008 at 9:27 am

    Can arrow keys disable? I do not know how to do this.

  5. 5 manovotny Aug 19th, 2008 at 12:59 pm

    PEFRECT! This solves half of my problem… Care to share if you have a way to prevent someone from doing a copy/paste of text with line feeds in it? Solve that and I’ll be home free!

  6. 6 peterd Aug 19th, 2008 at 8:57 pm

    manovotny,

    The code is a bit rough, but does the following work for you?

    <?xml version="1.0" encoding="utf-8"?>
    <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();
                    }
                }
                private function textArea_change(evt:Event):void {
                    textArea.text = textArea.text.replace(/\n|\r/ig, "");
                }
            ]]>
        </mx:Script>
    
        <mx:TextArea id="textArea"
                text="The quick brown fox jumped over the lazy dog."
                verticalScrollPolicy="on"
                width="100%"
                height="100%"
                textInput="textArea_textInput(event);"
                change="textArea_change(event);" />
    
    </mx:Application>
    

    Peter

  7. 7 Prashant Khanal Dec 24th, 2008 at 3:22 am

    hey guys,
    i want to prevent linefeed using ENTER key rather i want to apply line feed using SHIFT + ENTER key. How do i do that?
    any hint?

    thanks in advance

  8. 8 Prashant Khanal Dec 24th, 2008 at 3:39 am

    hey guys,
    sorry to bother you
    looks like i found a workaround for that
    just used a keylistener as said by jexchan

  9. 9 Anonymous Mar 21st, 2009 at 2:46 pm
    textInput="if(event.text.indexOf('\n')!=-1)event.preventDefault();"
    

    come on … it is one line code

  10. 10 Vshar May 12th, 2009 at 7:48 am

    Hi Peter,

    If you can help me - I am working on a chat application - On Enter I want to send the text and on Shift + Enter I want to insert a line feed in the text area where the sender types message. The solution above inserts a linefeed after the whole text, does not break it into multiple lines.

    Thanks

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;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed