-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular-bsn.js
More file actions
35 lines (31 loc) · 930 Bytes
/
angular-bsn.js
File metadata and controls
35 lines (31 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
angular.module('angular-bsn', [])
/* Validates bsn numbers with the 11-proef */
.directive('bsn', [function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, $ngModelCtrl) {
$ngModelCtrl.$parsers.push(function(viewValue) {
var bsn = viewValue;
var rest = viewValue;
var sum = 0;
$ngModelCtrl.$setValidity('bsn', false);
if (bsn.length != 9) {
console.log('Invalid BSN: not 9 characters');
} else {
var sum = 0;
for (var i = 0; i < 8; i++) {
sum += (9 - i) * parseInt(bsn.charAt(i));
}
sum -= parseInt(bsn.charAt(8));
if (((sum % 11) == 0)) {
$ngModelCtrl.$setValidity('bsn', true);
} else {
console.log('Invalid BSN: 11-proef is not valid');
}
}
return viewValue;
});
}
};
}]);