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:
<?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.



Nice example, Have you ever tried dragging text into a textInput or TextArea control before?
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.
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
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…
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
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.
How can I exclude &.
Thanks
Using MXML:
<mx:TextInput id="textInput" restrict="^&" />Using ActionScript:
private var textInput:TextInput; private function init():void { textInput = new TextInput(); textInput.restrict = "^&"; addChild(textInput); }Peter