15
May
08

Restricting which characters a user can type in a TextInput control in Flex

The following example shows how you can restrict which characters a user can enter into a Flex TextInput control by setting the restrict property.

Full code after the jump.

The following example sets the restrict property to “0-9\-”, which only allows numbers and the hyphen (-) character:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/05/15/restricting-which-characters-a-user-can-type-in-a-textinput-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function textInput_change(evt:Event):void {
                arrColl.addItem(evt);
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arrColl" />

    <mx:ApplicationControlBar dock="true">
        <mx:TextInput id="textInput"
                restrict="0-9\-"
                change="textInput_change(event);" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            width="100%"
            height="100%" />

</mx:Application>

View source is enabled in the following example.


5 Responses to “Restricting which characters a user can type in a TextInput control in Flex”


  1. 1 Jason May 16th, 2008 at 12:52 am

    Nice example, Have you ever tried dragging text into a textInput or TextArea control before?

  2. 2 coco May 16th, 2008 at 1:02 am

    hi,peter,I need you help,can you give me a example about displaying word or excel in flex? I have no idea,please give me some advice,thank you in advanced.

  3. 3 Om Muppirala May 16th, 2008 at 10:21 am

    I have discussed an edge case in restricting characters in a TextInput control here:
    http://www.bigosmallm.com/2008/03/how-to-restrict-backslash-character.html

  4. 4 stream May 16th, 2008 at 7:04 pm

    I only a little English, it may not clear expression.
    i found a bug of PopUpButton.
    Can you help me?

    pop is useful in the Application of tag.

    But…. PopUpButton is invalid in the TitleWindow tag

    i need you help for me, please…

  5. 5 peterd May 19th, 2008 at 8:24 am

    stream,

    I’ve filed a bug for the issue at http://bugs.adobe.com/jira/browse/SDK-15590 . Feel free to add any other comments to the bug, or subscribe/watch the bug for future updates.

    Thanks,
    Peter

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