Created
December 8, 2022 11:35
-
-
Save marceloag/781625e1736870dc8259def298bc6bb4 to your computer and use it in GitHub Desktop.
Funcion que valida el rut chileno en Javascript
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 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