Skip to content

Instantly share code, notes, and snippets.

@AlexR1712
Last active October 2, 2024 20:19
Show Gist options
  • Save AlexR1712/60fd0a643fa52b29d7fd2417cef3e1f4 to your computer and use it in GitHub Desktop.
Save AlexR1712/60fd0a643fa52b29d7fd2417cef3e1f4 to your computer and use it in GitHub Desktop.
Script to Validate SENIAT RIF number in Venezuela
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