Flex中TextInput和TextArea有一個比較有用的屬性restrict(限制,限定),看下面例子:
1,<mx:extInput id="test_ti" width="160" maxChars="20" restrict="0-9" text="0"/>
這樣,這個輸入框最多隻能輸入20個字元,隻能輸入0到9之間的數字了,你如果輸入别的是輸入不進去的
2,<mx:extInput id="test_ti" width="160" maxChars="20" restrict="0-9\." text="0"/>
這樣,輸入框可以輸入0到9之間的數字,以及輸入'.',中間必須用'\'分隔開來
3,<mx:extInput id="test_ti" width="160" restrict="0-9\ab" text="0"/>這樣,輸入框可以輸入0到9之間的數字,以及a,或b
4,<mx:extInput id="test_ti" width="160" restrict="a-z" text="0"/>
可以輸入a到z之間任何一個英文字母,'-'表示區間,如果要輸入'-',就必須加'\',如\-
結論:
用restrict有個好處,就是省去了驗證的麻煩,比如檢驗是否為數字,如果加了restrict="0-9",就不需要檢驗了,因為這個輸入框隻能輸入0到9之間的數字,别的輸不進去
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:x="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" backgroundColor="#A7E460">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:eclarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:eclarations>
<s:extInput x="167" y="190" restrict="0-9"/>
<s:abel x="70" y="190" text="隻能輸入數字" width="89" height="22" />
<s:abel x="70" y="234" text="隻能輸入字母" width="89" height="23"/>
<s:TextInput x="167" y="234" restrict="A-Z"/>
</s:Application>