src/cvc.js
'use strict'
var cvc = require('creditcards').cvc
var bind = require('function-bind')
module.exports = factory
factory.$inject = ['$parse']
function factory ($parse) {
return {
restrict: 'A',
require: 'ngModel',
compile: function (element, attributes) {
attributes.$set('maxlength', 4)
attributes.$set('pattern', '[0-9]*')
attributes.$set('xAutocompletetype', 'cc-csc')
return function (scope, element, attributes, ngModel) {
ngModel.$validators.ccCvc = function (value) {
return ngModel.$isEmpty(ngModel.$viewValue) || cvc.isValid(value, $parse(attributes.ccType)(scope))
}
if (attributes.ccType) {
scope.$watch(attributes.ccType, bind.call(ngModel.$validate, ngModel))
}
}
}
}
}