The Card Number Validation Problem
I worked on a Stripe integration for OctoPerf, our load testing tool during the past weeks.
Validating a credit card number is really complex. Thankfully Stripe comes with a JavaScript API to validate the various fields of a credit card. But an AngularJs integration is not that straightforward.
Note:
The method explained here can be used to validate the other fields of the credit card.
The Custom Directive Solution
We are going to create a custom directive that uses the Stripe JS to do the validation. The error messages are displayed via ng-messages.
Injecting Stripe
The first step is to inject Stripe. To do so we need to create a service that provides it:
|
|
Stripe is retrieved from the $window object. So we must include the Stripe Script in our HTML:
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
.
The Directive
Then we create a custom validation directive:
|
|
Here we add our cardNumber validator to the enclosing controller and delegate the job to the function Stripe.card.validateCardNumber
.
The HTML code
Finally on the HTML view we can create the following form:
|
|
Our stripe-card-number directive is added to the text input (we do not use a number input here).
Using ng-messages we can display an error message
when the validation fails. The ng-message="cardNumber"
value must match the name given in the stripeCardNumber directive ctrl.$validators.cardNumber
.
Ng-messages is a separate angular module. So we need to include the angular-messages script in our HTML and add ngMessages as one of our application dependencies.
Conclusion
We can easily do the same to validate the CVC:
|
|
|
|
And a direct solution is to use existing AngularJs modules: