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

 
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

8 Responses to Creating a credit card validator in ActionScript

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

  2. David Buhler says:

    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.

  3. peterd says:

    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

  4. Krishna says:

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

  5. Tony says:

    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.

  6. Oyunlar says:

    Thanks …

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

  8. Anonymous says:

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

Leave a Reply

Your email address will not be published.

You may 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