Uncategorized
Using the Flex CreditCardValidator
I spent some time yesterday fighting with this control because of a stupid assumption I made. I have used the Credit Card validation in the ColdFusion Flash forms on several occasions. As such you just specify that a field should be validated as “creditcard” and you move on. It checks to make sure its of appropriate length and looks like a credit card number.
So as I began my day validating credit card numbers in flex, I assumed that the function was very similar. So I created my mx:CreditCardValidator component in mxml. I set the cardNumberSource and cardNumber properties to my text field. And then I ran my application. I tested validation with nothing in the box, and it told me the field was required. I then tested that putting letters in the box also caused the appropriate error. Then I next tested that putting in an invalid card number also caused an error. But it did not. Of course I assumed I had missed some default value that suggests that valid numbers are not required, but that was not the case. I fought with it for quite a while before making realizing the problem.
From the Flex Developers Guide:
The CreditCardValidator class validates that a credit card number is the correct length, has the correct prefix, and passes the Luhn mod10 algorithm for the specified card type. This validator does not check whether the credit card is an actual active credit card account.
I read over that block several times in a couple different places, but I never read it quite right. It does not just validate the number, it validates the credit card number is correct based on the type of card selected. So the CreditCardValidator also has properties for cardTypeSource and cardTypeProperty. And without setting those, and without the values of those being from the CreditCardValidatorCardType.[card type] static variables, the CreditCardValidator class can not actually validate the number.
I fought with this for quite a bit because I read the docs as it “can” validate card type, not it “must” validate card type. So keep an eye out and dont fall into the same trap I did.
25 Jan 2007 Simeon
