Created
November 19, 2013 16:26
-
-
Save nesheroj/7548079 to your computer and use it in GitHub Desktop.
Angular directive for validating a spanish bank account number (SICA)
This file contains hidden or 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
| 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