Created
December 3, 2015 21:49
-
-
Save jmonterroso/7bbd7462d2c08244b4d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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