Last active
October 2, 2024 20:19
-
-
Save AlexR1712/60fd0a643fa52b29d7fd2417cef3e1f4 to your computer and use it in GitHub Desktop.
Script to Validate SENIAT RIF number in Venezuela
This file contains 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
const NATIONALITY_ARRAY = { | |
'V': '1', | |
'E': '2', | |
'J': '3', | |
'P': '4', | |
'G': '5', | |
'C': '3' | |
}; | |
const MULTIPLIERS = [4, 3, 2, 7, 6, 5, 4, 3, 2]; | |
function validateRif(value) { | |
if (typeof value !== 'string') { | |
return false; | |
} | |
// Convert value to uppercase | |
const upperValue = value.toUpperCase(); | |
// Check if the RIF has a valid format | |
const rifPattern = /^[VEPJGC]-?\d{8}-?\d$/i; | |
if (!rifPattern.test(upperValue)) { | |
return false; | |
} | |
// Remove dashes from the RIF | |
const fullRif = upperValue.replace(/-/g, ''); | |
const contributor = fullRif.slice(0, -1); | |
const expectedValidationNumber = fullRif.slice(-1); | |
return expectedValidationNumber === calculateValidationNumber(contributor); | |
} | |
function calculateValidationNumber(rif) { | |
// Replace the first character with its numeric value | |
const numericRif = NATIONALITY_ARRAY[rif[0]] + rif.slice(1); | |
// Calculate the weighted sum | |
const sum = Array.from(numericRif).reduce((acc, digit, index) => { | |
return acc + parseInt(digit, 10) * MULTIPLIERS[index]; | |
}, 0); | |
// Calculate the validation number | |
const remainder = sum % 11; | |
const difference = 11 - remainder; | |
// Return the validation number | |
return (difference > 9) ? '0' : difference.toString(); | |
} | |
// Example usage: | |
const value = 'V-12345678-9'; | |
const isValid = validateRif(value); | |
console.log(`Is the RIF valid? ${isValid}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment