Skip to content

Instantly share code, notes, and snippets.

@julekgwa
Last active July 14, 2023 08:01
Show Gist options
  • Save julekgwa/eb64cd4e0e72c8ae6851a3832b240aa6 to your computer and use it in GitHub Desktop.
Save julekgwa/eb64cd4e0e72c8ae6851a3832b240aa6 to your computer and use it in GitHub Desktop.
South African ID validation
function validateZARID(id) {
if (!id || typeof id !== 'string') {
return;
}
id = id.split('');
const validationCheck = [];
let sum = 0;
for (let i = 0; i < id.length; i++) {
// double every second digit.
if (i % 2 === 1) {
const multiplyNumber = Number(id[i]) * 2;
// check for double numbers
if (multiplyNumber && multiplyNumber.toString().length > 1) {
const [n1, n2] = multiplyNumber.toString().split('');
const singleDigit = Number(n1) + Number(n2);
validationCheck.push(singleDigit);
continue;
}
validationCheck.push(multiplyNumber);
continue;
}
validationCheck.push(Number(id[i]));
}
for (let i = 0; i < validationCheck.length; i++) {
sum += validationCheck[i];
}
return sum === 0 ? false : !(sum % 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment