From b52d06c40db3b53699c7011eedd306aa951f1201 Mon Sep 17 00:00:00 2001 From: Chai Botta Date: Fri, 22 Jan 2021 19:05:43 +0100 Subject: [PATCH] aggiunta validazione codice fiscale con regex considerando anche omocodie --- package-lock.json | 2 +- src/codice-fiscale.js | 3 +++ tests/index.spec.js | 7 ++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index a85cd32..5ebf323 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "codice-fiscale-js", - "version": "2.3.6", + "version": "2.3.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/codice-fiscale.js b/src/codice-fiscale.js index bc4544a..c1aec4d 100755 --- a/src/codice-fiscale.js +++ b/src/codice-fiscale.js @@ -83,6 +83,9 @@ class CodiceFiscale { if (cf.length !== 16) { return false } + if(! /^[A-Z]{6}[0-9LMNPQRSTUV]{2}[ABCDEHLMPRST]{1}[0-9LMNPQRSTUV]{2}[A-Z]{1}[0-9LMNPQRSTUV]{3}[A-Z]{1}$/.test(cf)){ + return false; + } const expectedCheckCode = codiceFiscale.charAt(15) cf = codiceFiscale.slice(0, 15) return CodiceFiscale.getCheckCode(cf).toUpperCase() === expectedCheckCode.toUpperCase(); diff --git a/tests/index.spec.js b/tests/index.spec.js index 551ee12..e0a5585 100755 --- a/tests/index.spec.js +++ b/tests/index.spec.js @@ -442,4 +442,9 @@ describe("La classe Comune", ()=>{ expect(bz.nome).toEqual('BOLZANO') }) -}) \ No newline at end of file +}) + +let invalidCfis = "BLIPTR93MO4A674Q"; +test('check invalid cfis by regex control', () => { + expect(CodiceFiscale.check(invalidCfis)).toEqual(false); +}); \ No newline at end of file