Creating a credit card validator in ActionScript

by Peter deHaan on October 19, 2007

in ActionScript, CreditCardValidator, CreditCardValidatorCardType

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.

View MXML

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

{ 8 comments… read them below or add one }

1 Alexandre Madurell October 21, 2007 at 2:50 am

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!!!

Reply

2 David Buhler October 25, 2007 at 10:30 am

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.

Reply

3 peterd October 25, 2007 at 10:43 am

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

Reply

4 Krishna November 14, 2007 at 6:22 am

is This is built in validator or what?ok suppose if we want to validdate combobox how to do that?

Reply

5 Tony March 25, 2008 at 1:12 pm

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.

Reply

6 Oyunlar April 29, 2008 at 2:54 am

Thanks …

Reply

7 Prime Credit Cards December 15, 2008 at 4:06 pm

Excellent snippet, I could have used that credit card validator before, I tried working on something similar with little success. Thanks, again!

Reply

8 Anonymous March 30, 2009 at 5:50 am

i want cc data cvv wathi fullinfo+ this is my id saleem00726@yahoo.com

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: