Last active
January 24, 2024 09:00
-
-
Save brokenthorn/dc4cadd2ecfcbb57bf6bdb7721a83239 to your computer and use it in GitHub Desktop.
Revisions
-
brokenthorn revised this gist
Jan 19, 2022 . 1 changed file with 16 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,44 +6,58 @@ * @returns {string|boolean} `true` if the input is a valid CIF or an error message, * describing where validation failed */ function validateRomanianCIF (v) { if (typeof v !== 'string') { return 'Nu este un șir de caractere!' } let cif = v.toUpperCase() // remove RO from cif: const indexOfRo = cif.indexOf('RO') if (indexOfRo > -1) { cif = cif.substr(0, indexOfRo) + cif.substr(indexOfRo + 2) } // remove spaces: cif = cif.replace(' ', '') // validate character length: if (cif.length < 2 || cif.length > 10) { return 'Lungimea corectă fără RO, este între 2 și 10 caractere!' } // validate that so far the resulting CIF looks like an integer value: if (Number.isNaN(parseInt(cif))) { return 'Nu este număr!' } // begin testing: const testKey = '753217532' const controlNumber = parseInt(cif.substr(cif.length - 1)) // remove control number: cif = cif.substr(0, cif.length - 1) // pad left with zeros to reach testKey length: while (cif.length !== testKey.length) { cif = '0' + cif } let sum = 0 let i = cif.length while (i--) { sum = sum + (cif.charAt(i) * testKey.charAt(i)) } let calculatedControlNumber = sum * 10 % 11 if (calculatedControlNumber === 10) { calculatedControlNumber = 0 } return controlNumber === calculatedControlNumber || `CIF invalid! Cifra de control calculată (${calculatedControlNumber}) diferă de cea introdusă (${controlNumber})!` } -
brokenthorn revised this gist
Mar 16, 2020 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ /** * Validates if a string conforms to a valid Romanian Fiscal Code. * * See Romanian Law no. 359 from 8 September 2004. * @param v {string} input string to be validated * @returns {string|boolean} `true` if the input is a valid CIF or an error message, * describing where validation failed @@ -42,5 +44,6 @@ if (calculatedControlNumber === 10) { calculatedControlNumber = 0 } return controlNumber === calculatedControlNumber || `CIF invalid! Cifra de control calculată (${calculatedControlNumber}) diferă de cea introdusă (${controlNumber})!` } -
brokenthorn created this gist
Mar 16, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ /** * Validates if a string conforms to a valid Romanian Fiscal Code. * @param v {string} input string to be validated * @returns {string|boolean} `true` if the input is a valid CIF or an error message, * describing where validation failed */ function romanianCifValidate (v) { if (typeof v !== 'string') { return 'Nu este un șir de caractere!' } let cif = v.toUpperCase() // remove RO from cif: const indexOfRo = cif.indexOf('RO') if (indexOfRo > -1) { cif = cif.substr(0, indexOfRo) + cif.substr(indexOfRo + 2) } // remove spaces: cif = cif.replace(' ', '') // validate character length: if (cif.length < 2 || cif.length > 10) { return 'Lungimea corectă fără RO, este între 2 și 10 caractere!' } // validate that so far the resulting cif looks like an integer value: if (Number.isNaN(parseInt(cif))) { return 'Nu este număr!' } // begin testing: const testKey = '753217532' const controlNumber = parseInt(cif.substr(cif.length - 1)) // remove control number: cif = cif.substr(0, cif.length - 1) // pad left with zeros to reach testKey length: while (cif.length !== testKey.length) { cif = '0' + cif } let sum = 0 let i = cif.length while (i--) { sum = sum + (cif.charAt(i) * testKey.charAt(i)) } let calculatedControlNumber = sum * 10 % 11 if (calculatedControlNumber === 10) { calculatedControlNumber = 0 } return controlNumber === calculatedControlNumber }