Skip to content

Instantly share code, notes, and snippets.

@marceloag
Created December 8, 2022 11:35
Show Gist options
  • Save marceloag/781625e1736870dc8259def298bc6bb4 to your computer and use it in GitHub Desktop.
Save marceloag/781625e1736870dc8259def298bc6bb4 to your computer and use it in GitHub Desktop.
Funcion que valida el rut chileno en Javascript
const validateRut = (value) => {
const cleanValue = value.replace(/\.|-/g, '');
const isValid = /^\d{7,8}[0-9kK]$/.test(cleanValue);
if (isValid) {
const rutBody = cleanValue.slice(0, -1);
const rutVerifier = cleanValue.slice(-1).toUpperCase();
const product = rutBody
.split('')
.reverse()
.map((digit, i) => digit * (i < 6 ? 2 + i : i-4))
.reduce((acc, curr) => acc + curr);
const verifier =
'0K98765432'.charAt(product % 11);
return verifier === rutVerifier;
}
return isValid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment