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

by Peter deHaan on May 15, 2008

in TextInput

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.

{ 11 comments… read them below or add one }

1 Jason May 16, 2008 at 12:52 am

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

Reply

2 coco May 16, 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.

Reply

3 Om Muppirala May 16, 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

Reply

4 stream May 16, 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…

Reply

5 peterd May 19, 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

Reply

6 missamma June 1, 2009 at 7:23 am

Can anypne pls tell me how to restrict the first character of the textInput..?

I mean user the should able to enter a string which starts with only alphabet..?

Regards
Missamma.

Reply

7 how can I exclude &? June 16, 2009 at 3:57 am

How can I exclude &.

Thanks

Reply

8 Peter deHaan June 16, 2009 at 6:50 am

Using MXML:

<mx:TextInput id="textInput"
        restrict="^&amp;" />

Using ActionScript:

private var textInput:TextInput;
private function init():void {
    textInput = new TextInput();
    textInput.restrict = "^&";
    addChild(textInput);
}

Peter

Reply

9 Saroj Panda September 15, 2009 at 11:53 pm

I have to do the validation that user can enter hyphen(-) only once in the textinput. Can anyone help me. Urgent!!

Reply

10 hardik November 19, 2009 at 2:03 am

how to restrict double quotes in a text input field

Reply

11 Peter deHaan November 19, 2009 at 3:08 pm

@hardik,

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 
    <mx:Script>
        <![CDATA[
            protected function init():void {
                txtInput2.restrict = "^\"";
 
                /* or */
 
                txtInput2.restrict = '^"';
            }
        ]]>
    </mx:Script>
 
    <mx:TextInput id="txtInput1" restrict="^&quot;" />
    <mx:TextInput id="txtInput2" initialize="init();" />
 
</mx:Application>

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: