Preedagrace's Blog

Just another WordPress.com weblog

Currency validators

Posted by preedagrace on June 19, 2009

currency validators

The CurrencyValidator class ensures that a String represents a valid currency expression. It can make sure the input falls within a given range (specified by minValue and maxValue), is non-negative (specified by allowNegative), and does not exceed the specified precision. The CurrencyValidator class correctly validates formatted and unformatted currency expressions, e.g., “$12,345.00″ and “12345″. You can customize the currencySymbol, alignSymbol, thousandsSeparator, and decimalSeparator properties for internationalization.

codings

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”>

<mx:Script>
<![CDATA[

import mx.events.ValidationResultEvent;
private var vResult:ValidationResultEvent;

private function Format():void {

vResult = numVal.validate();

if (vResult.type==ValidationResultEvent.VALID) {
var temp:Number=Number(priceUS.text);
formattedUSPrice.text= usdFormatter.format(temp);
}

else {
formattedUSPrice.text="";
}
}
]]>
</mx:Script>

<mx:CurrencyFormatter id=”usdFormatter” precision=”2″
currencySymbol=”$” decimalSeparatorFrom=”.”
decimalSeparatorTo=”.” useNegativeSign=”true”
useThousandsSeparator=”true” alignSymbol=”left”/>

<mx:NumberValidator id=”numVal” source=”{priceUS}” property=”text”
allowNegative=”true” domain=”real”/>

<mx:Panel title=”CurrencyFormatter Example” width=”75%” height=”75%”
paddingTop=”10″ paddingLeft=”10″ paddingRight=”10″ paddingBottom=”10″>

<mx:Form>
<mx:FormItem label=”Enter U.S. dollar amount:”>
<mx:TextInput id=”priceUS” text=”" width=”50%”/>
</mx:FormItem>

<mx:FormItem label=”Formatted amount: “>
<mx:TextInput id=”formattedUSPrice” text=”" width=”50%” editable=”false”/>
</mx:FormItem>

<mx:FormItem>
<mx:Button label=”Validate and Format” click=”Format();”/>
</mx:FormItem>
</mx:Form>

</mx:Panel>
</mx:Application>

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.