Search This Blog

2009-04-14

Validating Integers and Real number in Flex

The following example shows how you can validate a number as an integer or real (floating point)

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/30/validating-integers-using-the-numbervalidator-class/ -->
<mx:Application name="NumberValidator_domain_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">

<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ValidationResultEvent;

private function numberValidator_invalid(evt:ValidationResultEvent):void {
Alert.show(evt.message);
}

private function numberValidator_valid(evt:ValidationResultEvent):void {
Alert.show(evt.type);
}
]]>
</mx:Script>

<mx:NumberValidator id="numberValidator"
domain="{comboBox.selectedItem}"
source="{textInput}"
property="text"
trigger="{button}"
triggerEvent="click"
invalid="numberValidator_invalid(event);"
valid="numberValidator_valid(event);" />

<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="domain:">
<mx:ComboBox id="comboBox"
dataProvider="[real,int]" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>

<mx:Form defaultButton="{button}">
<mx:FormItem direction="horizontal">
<mx:TextInput id="textInput"
restrict="0-9\.\-"
maxChars="10" />
<mx:Button id="button"
label="validate" />
</mx:FormItem>
</mx:Form>

</mx:Application>

No comments: