A loyal reader asked in a comment how one could go about creating a credit card validator using ActionScript instead of MXML. Peter loves his readers, so behold, my solution…
The following example shows how you can create a credit card validator using the CreditCardValidator and CreditCardValidatorCardType classes.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/10/19/creating-a-credit-card-validator-in-actionscript/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init();">
<mx:Style>
global {
modalTransparencyBlur: 0;
modalTransparency: 0.4;
modalTransparencyDuration: 250;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ValidationResultEvent;
import mx.styles.StyleManager;
import mx.validators.CreditCardValidator;
import mx.validators.CreditCardValidatorCardType;
private var ccVal:CreditCardValidator;
private var globalStyle:CSSStyleDeclaration;
private function init():void {
/* Initialize credit card validator. */
ccVal = new CreditCardValidator();
ccVal.cardNumberSource = cc;
ccVal.cardNumberProperty = "cardNumber";
ccVal.cardTypeSource = cc;
ccVal.cardTypeProperty = "cardType";
ccVal.addEventListener(ValidationResultEvent.VALID, isValid);
ccVal.addEventListener(ValidationResultEvent.INVALID, isInvalid);
/* Create reference to global style. */
globalStyle = StyleManager.getStyleDeclaration("global");
}
private function isValid(evt:ValidationResultEvent):void {
globalStyle.setStyle("modalTransparencyColor", "green");
Alert.show("you're valid", evt.type);
}
private function isInvalid(evt:ValidationResultEvent):void {
globalStyle.setStyle("modalTransparencyColor", "red");
Alert.show(evt.message, evt.type);
}
]]>
</mx:Script>
<mx:Model id="cc">
<card>
<cardNumber>{cardNumber.text}</cardNumber>
<cardType>{cardType.selectedItem.data}</cardType>
</card>
</mx:Model>
<mx:Array id="ccType">
<mx:Object label="American Express"
data="{CreditCardValidatorCardType.AMERICAN_EXPRESS}" />
<mx:Object label="Diners Club"
data="{CreditCardValidatorCardType.DINERS_CLUB}" />
<mx:Object label="Discover"
data="{CreditCardValidatorCardType.DISCOVER}" />
<mx:Object label="Master Card"
data="{CreditCardValidatorCardType.MASTER_CARD}" />
<mx:Object label="Visa"
data="{CreditCardValidatorCardType.VISA}" />
</mx:Array>
<mx:Form>
<mx:FormItem label="Card number:" required="true">
<mx:TextInput id="cardNumber"
restrict="[0-9]" />
</mx:FormItem>
<mx:FormItem label="Card type:" required="true">
<mx:ComboBox id="cardType"
dataProvider="{ccType}"
prompt="Please select a credit card type."/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Validate"
click="ccVal.validate();" />
</mx:FormItem>
</mx:Form>
</mx:Application>
View source is enabled in the following example.
You can test the credit card validation with the following information:
Card number: 4111111111111111
Card type: Visa





Yoohooo!!!
Man! That comes in handy! Last year I had to program all that from scratch (with Flash and AS2) and I was having a lot of issues with Mastercard cards. Thanks a bunch!!!
Since the credit-card security code (3-4 digits, depending on the card) is part of most credit card transactions (online, of course), it might be a worthwhile addition to this example. I wish Adobe had included the credit-card security code as part of the CC validator.
David,
If you want, you can file an enhancement request in the public Flex bug base at http://bugs.adobe.com/flex/.
Project: Flex SDK
Issue type: Enhancement
Component/s: Validators
Peter
is This is built in validator or what?ok suppose if we want to validdate combobox how to do that?
All the easy stuff is done in ActionScript; the hard part, the part that uses the mx:Model tag for the card number and type source and property, doesn’t appear to be trivial in ActionScript, and isn’t shown how to be done.
Thanks …