Last active
January 24, 2024 09:00
-
-
Save brokenthorn/dc4cadd2ecfcbb57bf6bdb7721a83239 to your computer and use it in GitHub Desktop.
Functie Javascript pentru validarea CIF (Cod Identificare Fiscala) / CUI (Cod Unic Identificare)
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
/** | |
* 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 | |
*/ | |
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})!` | |
} |
Cu plăcere.
Salut, mersi de functie.
Daca imi permiti ai mai jos o varianta actualizata:
validateRomanianCIF(v) {
if (typeof v !== 'string') {
return 'Nu e string!'
}
let cif = v.toUpperCase();
// remove RO from cif:
cif = (cif.indexOf('RO') > -1) ? cif.substring(2) : cif;
// remove spaces:
cif = cif.replace(/\s/g,'');
// 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.substring(cif.length - 1));
// remove control number:
cif = cif.substring(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 + (parseInt(cif.charAt(i)) * parseInt(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})!`
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Mulțam