Skip to content

Instantly share code, notes, and snippets.

@brokenthorn
Last active January 24, 2024 09:00
Show Gist options
  • Save brokenthorn/dc4cadd2ecfcbb57bf6bdb7721a83239 to your computer and use it in GitHub Desktop.
Save brokenthorn/dc4cadd2ecfcbb57bf6bdb7721a83239 to your computer and use it in GitHub Desktop.

Revisions

  1. brokenthorn revised this gist Jan 19, 2022. 1 changed file with 16 additions and 2 deletions.
    18 changes: 16 additions & 2 deletions romanian-cif-validate.js
    Original 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 romanianCifValidate (v) {
    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:

    // 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})!`
    }
  2. brokenthorn revised this gist Mar 16, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion romanian-cif-validate.js
    Original 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
    return controlNumber === calculatedControlNumber ||
    `CIF invalid! Cifra de control calculată (${calculatedControlNumber}) diferă de cea introdusă (${controlNumber})!`
    }
  3. brokenthorn created this gist Mar 16, 2020.
    46 changes: 46 additions & 0 deletions romanian-cif-validate.js
    Original 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
    }