Alex Lindgren in Technology 5 minute read

Custom AngularJS directive for Luhn validation

The Luhn algorithm is a simple checksum formula that can be used to validate credit card and other numbers. I thought it would be good to write a custom AngularJS directive to validate text inputs using the Luhn algorithm. I am new to AngularJS, so this was a good exercise in understanding how to implement custom directives.

First, I found this simple JavaScript implementation of the Luhn validation algorithm. I then found this example on Plunker of a custom AngularJS directive. I modified the Plunk to create an AngularJS directive that validates a text input with the Luhn algorithm.

Here is my Plunk (click the Code tab to view the source):

As I said, I am new to AngularJS, so feedback and sussgestions are welcome!