Skip to content

pushinn/arg-id-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Argentina CUIL/CUIT JS validation.

Inspired by

Valid examples:

  1. 30-71492781-3
  2. 30-70843653-0
function processDni(value) {
    const ALLOWED_TYPES_REGEX = /^(20|23|24|27|30|33|34)\d{9}$/;
    
    function format(n) {
        return n.replaceAll(/\D/g, '');
    }

    function validateCheckDigit(n) {
        let multipliers = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
        let number = n.slice(0, -1);
        let type = n.slice(0, 2);

        let sum = 0;
        for (let i = 0; i < 10; i++) {
            sum += multipliers[i] * number[i];
        }

        let checkDigit = 11 - sum % 11;
        if (checkDigit == 11) {
            checkDigit = 0;
        } else if (checkDigit == 10) {
            /**
             * I am not sure about this section as it has never been tested :)
             * Some algorithms just consider such CUIL/CUIT as invalid
             * */
            let dni = extractRawDni(n);
            switch (type) {
                case 20:
                    return validateCheckDigit('23' + dni + '9')
                case 27:
                    return validateCheckDigit('23' + dni + '4')
                case 24:
                    return validateCheckDigit('23' + dni + '3')
                case 30:
                    return validateCheckDigit('33' + dni + '9')
                case 34:
                    return validateCheckDigit('33' + dni + '3')
                default:
                    return false
            }
        }
        return checkDigit == n.slice(-1)
    }

    function extractRawDni(val) {
        return String(val).slice(2, -1);
    }

    value = format(value);

    return {
        dni: extractRawDni(value),
        valid: ALLOWED_TYPES_REGEX.test(value) && validateCheckDigit(value)
    }
}

P.S. The code can be improved and simplified for sure. Sorry guys, I am not a JS developer :shipit:

Releases

No releases published

Packages

No packages published