<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex Examples &#187; CreditCardValidator</title>
	<atom:link href="http://blog.flexexamples.com/category/creditcardvalidator/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a credit card validator in ActionScript</title>
		<link>http://blog.flexexamples.com/2007/10/19/creating-a-credit-card-validator-in-actionscript/</link>
		<comments>http://blog.flexexamples.com/2007/10/19/creating-a-credit-card-validator-in-actionscript/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 06:26:08 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[CreditCardValidator]]></category>
		<category><![CDATA[CreditCardValidatorCardType]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/10/19/creating-a-credit-card-validator-in-actionscript/</guid>
		<description><![CDATA[<p>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&#8230;</p> <p>The following example shows how you can create a credit card validator using the CreditCardValidator and CreditCardValidatorCardType classes.</p> <p>Full code after the jump.</p> <p></p> <p [...]]]></description>
			<content:encoded><![CDATA[<p>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&#8230;</p>
<p>The following example shows how you can create a credit card validator using the CreditCardValidator and CreditCardValidatorCardType classes.</p>
<p>Full code after the jump.</p>
<p><span id="more-243"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/CreditCardValidator_validate_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/10/19/creating-a-credit-card-validator-in-actionscript/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Style&gt;
        global {
            modalTransparencyBlur: 0;
            modalTransparency: 0.4;
            modalTransparencyDuration: 250;
        }
    &lt;/mx:Style&gt;

    &lt;mx:Script&gt;
        &lt;![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);
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Model id="cc"&gt;
        &lt;card&gt;
            &lt;cardNumber&gt;{cardNumber.text}&lt;/cardNumber&gt;
            &lt;cardType&gt;{cardType.selectedItem.data}&lt;/cardType&gt;
        &lt;/card&gt;
    &lt;/mx:Model&gt;

    &lt;mx:Array id="ccType"&gt;
        &lt;mx:Object label="American Express"
                data="{CreditCardValidatorCardType.AMERICAN_EXPRESS}" /&gt;
        &lt;mx:Object label="Diners Club"
                data="{CreditCardValidatorCardType.DINERS_CLUB}" /&gt;
        &lt;mx:Object label="Discover"
                data="{CreditCardValidatorCardType.DISCOVER}" /&gt;
        &lt;mx:Object label="Master Card"
                data="{CreditCardValidatorCardType.MASTER_CARD}" /&gt;
        &lt;mx:Object label="Visa"
                data="{CreditCardValidatorCardType.VISA}" /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:Form&gt;
        &lt;mx:FormItem label="Card number:" required="true"&gt;
            &lt;mx:TextInput id="cardNumber"
                    restrict="[0-9]" /&gt;
        &lt;/mx:FormItem&gt;
        &lt;mx:FormItem label="Card type:" required="true"&gt;
            &lt;mx:ComboBox id="cardType"
                    dataProvider="{ccType}"
                    prompt="Please select a credit card type."/&gt;
        &lt;/mx:FormItem&gt;
        &lt;mx:FormItem&gt;
        &lt;mx:Button label="Validate"
                click="ccVal.validate();" /&gt;
        &lt;/mx:FormItem&gt;
    &lt;/mx:Form&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/CreditCardValidator_validate_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/CreditCardValidator_validate_test/bin/main.html" width="100%" height="200"></iframe></p>
<p class="construction">You can test the credit card validation with the following information:<br />
Card number: 4111111111111111<br />
Card type: Visa</p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Creating a credit card validator in ActionScript on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/10/19/creating-a-credit-card-validator-in-actionscript/',contentID: 'post-243',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: '',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/10/19/creating-a-credit-card-validator-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

