Last active
August 31, 2015 14:29
-
-
Save matthewbednarski/5a8ba73e9e89bca20449 to your computer and use it in GitHub Desktop.
An angular filter for validating Italian Codici Fiscali
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
| (function() { | |
| var app = angular.module('cf', []) | |
| .filter('isCodiceFiscale', [codiceFiscale]); | |
| /** | |
| * taken from https://gist.github.com/TheHiddenHaku/6951634 | |
| * | |
| * A function for evaluating if a string is a valid codice fiscale | |
| * | |
| * @param String codiceFiscale | |
| * | |
| * @returns true if is a valid codice fiscale, otherwise false | |
| */ | |
| function codiceFiscale(cfins) { | |
| var cf = cfins.toUpperCase(); | |
| var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/; | |
| if (!cfReg.test(cf)) { | |
| return false; | |
| } | |
| var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX"; | |
| var s = 0; | |
| for (i = 1; i <= 13; i += 2) { | |
| s += setpari.indexOf(set2.charAt(set1.indexOf(cf.charAt(i)))); | |
| } | |
| for (i = 0; i <= 14; i += 2) { | |
| s += setdisp.indexOf(set2.charAt(set1.indexOf(cf.charAt(i)))); | |
| } | |
| if (s % 26 != cf.charCodeAt(15) - 'A'.charCodeAt(0)) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment