Skip to content

Instantly share code, notes, and snippets.

@nesheroj
Created November 19, 2013 16:26
Show Gist options
  • Select an option

  • Save nesheroj/7548079 to your computer and use it in GitHub Desktop.

Select an option

Save nesheroj/7548079 to your computer and use it in GitHub Desktop.
Angular directive for validating a spanish bank account number (SICA)
define(['app/main'], function (app) {
app.register.directive('isSica', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elem, attr, ngModel) {
function controlDigit(input) {
var weights = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6];
var sum = 0;
for (var i = 0; i < weights.length; i++) {
sum += Number(input.charAt(i)) * weights[i];
}
switch (11 - (sum % 11)) {
case 11: return 0;
case 10: return 1;
default: return 11 - sum % 11;
}
}
function isValidSICA(sica) {
return sica.Entity != undefined && sica.Entity.length === 4 &&
sica.Branch != undefined && sica.Branch.length === 4 &&
sica.ControlDigit != undefined && sica.ControlDigit.length === 2 &&
sica.Account != undefined && sica.Account.length === 10 &&
sica.ControlDigit === (controlDigit("00" + sica.Entity + sica.Branch).toString() + controlDigit(sica.Account).toString());
}
scope.$watch(attr.isSica, function (newVal, oldVal) {
if (newVal != undefined) ngModel.$setValidity('isSica', isValidSICA(newVal));
}, true);
}
};
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment