Created
November 19, 2013 16:22
-
-
Save nesheroj/7547978 to your computer and use it in GitHub Desktop.
Angular Validation directives for Spain's National IDs
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('isDniNie', function () { | |
| return { | |
| restrict: 'A', | |
| require: 'ngModel', | |
| link: function (scope, elem, attr, ngModel) { | |
| var charsDNINIE = 'TRWAGMYFPDXBNJZSQVHLCKE'; | |
| function isValidDNINIE(input) { | |
| input = input.replace("-", "").toUpperCase(); | |
| switch (input.charAt(0)) { | |
| case 'X': | |
| input = '0' + input.substr(1); | |
| break; | |
| case 'Y': | |
| input = '1' + input.substr(1); | |
| break; | |
| case 'Z': | |
| input = '2' + input.substr(1); | |
| break; | |
| } | |
| return !isNaN(Number(input.substr(0, 8))) && charsDNINIE.charAt(Number(input.substr(0, 8)) % 23) === input.substr(8); | |
| } | |
| ngModel.$parsers.unshift(function (value) { | |
| var valid; | |
| if (value === undefined || value === null) valid = false; | |
| else valid = isValidDNINIE(value); | |
| ngModel.$setValidity('isDniNie', valid); | |
| return valid ? value : undefined; | |
| }); | |
| } | |
| }; | |
| }); | |
| app.register.directive('isDniNieNif', function () { | |
| return { | |
| restrict: 'A', | |
| require: 'ngModel', | |
| link: function (scope, elem, attr, ngModel) { | |
| var charsDNINIE = 'TRWAGMYFPDXBNJZSQVHLCKE'; | |
| var charsNIF = 'JABCDEFGHIJ'; | |
| function isValidDNINIENIF(input) { | |
| input = input.replace("-", "").toUpperCase(); | |
| switch (input.charAt(0)) { | |
| case 'X': | |
| return isValidDNINIE('0' + input.substr(1)); | |
| case 'Y': | |
| return isValidDNINIE('1' + input.substr(1)); | |
| case 'Z': | |
| return isValidDNINIE('2' + input.substr(1)); | |
| case 'N': | |
| case 'P': | |
| case 'Q': | |
| case 'R': | |
| case 'S': | |
| case 'F': | |
| case 'V': | |
| case 'W': | |
| return isValidNIF(input.substr(1, 7) + charsNIF.indexOf(input.charAt(8))); | |
| case 'A': | |
| case 'B': | |
| case 'C': | |
| case 'D': | |
| case 'E': | |
| case 'F': | |
| case 'G': | |
| case 'H': | |
| case 'J': | |
| case 'U': | |
| return isValidNIF(input.substr(1)); | |
| default: | |
| return isValidDNINIE(input); | |
| } | |
| } | |
| function isValidDNINIE(input) { | |
| return !isNaN(Number(input.substr(0, 8))) && charsDNINIE.charAt(Number(input.substr(0, 8)) % 23) === input.substr(8); | |
| } | |
| function isValidNIF(input) { | |
| if (isNaN(Number(input))) return false; | |
| var sum = 0; | |
| for (var i = 0; i < 7; i++) { | |
| if (i % 2 === 1) sum += Number(input.charAt(i)); | |
| else { | |
| var odd = Number(input.charAt(i)) * 2; | |
| if (odd > 9) odd = 1 + (odd - 10); | |
| sum += odd; | |
| } | |
| } | |
| return ((10 - (sum % 10)) === 10 ? 0 : (10 - (sum % 10))) === Number(input.substr(7)); | |
| } | |
| ngModel.$parsers.unshift(function (value) { | |
| var valid = false; | |
| if (value != undefined) | |
| valid = isValidDNINIENIF(value); | |
| ngModel.$setValidity('isDniNieNif', valid); | |
| return valid ? value : undefined; | |
| }); | |
| } | |
| }; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment