Skip to content

Instantly share code, notes, and snippets.

@jmonterroso
Created December 3, 2015 21:49
Show Gist options
  • Save jmonterroso/7bbd7462d2c08244b4d1 to your computer and use it in GitHub Desktop.
Save jmonterroso/7bbd7462d2c08244b4d1 to your computer and use it in GitHub Desktop.
stanceApp.directive('ccNumberValidator', function () {
/*
^(?:4[0-9]{12}(?:[0-9]{3})? # Visa
| 5[1-5][0-9]{14} # MasterCard
| 3[47][0-9]{13} # American Express
| 6(?:011|5[0-9]{2})[0-9]{12} # Discover
)$
*/
return {
require: 'ngModel',
link: function ($scope, element, attrs, ngModel) {
ngModel.$validators.ccNumber = function (v) {
if (!angular.isUndefined(v) && v) {
var cardNumber = String(v).replace(/[ -]/g, '');
var valid = /^(?:4[0-9]{2,12}(?:[0-9]{3})?|5[1-5][0-9]{2,14}|3[47][0-9]{2,13}|6(?:011|5[0-9]{2})[0-9]{2,12})$/.test(cardNumber);
ngModel.$setDirty(valid);
}
return v;
};
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment