Created
January 3, 2018 15:47
-
-
Save eaguad1337/741c28e278a7e41bbb27cd16cce95396 to your computer and use it in GitHub Desktop.
VeeValidate Chilean Rut // VeeValidate Validador Rut Chileno
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
/* | |
* import VeeValidate, { Validator } from 'vee-validate'; | |
* Validator.extend('rut', require('./VeeRutValidator')); | |
* Vue.use(VeeValidate); | |
*/ | |
module.exports = { | |
getMessage: (field, params, data) => `El campo ${field} no es un rut válido`, | |
validate: rut => { | |
if (typeof(rut) !== 'string') { | |
return false; | |
} | |
var cRut = rut.replace(/[\.-]/g, ''); | |
var cDv = cRut.charAt(cRut.length - 1).toUpperCase(); | |
var nRut = parseInt(cRut.substr(0, cRut.length - 1)); | |
if (isNaN(nRut)) { | |
return false; | |
} | |
var sum = 0; | |
var factor = 2; | |
nRut = nRut.toString(); | |
for (var i = nRut.length - 1; i >= 0; i--) { | |
sum = sum + nRut.charAt(i) * factor; | |
factor = (factor + 1) % 8 || 2; | |
} | |
let computedDv = 0; | |
switch (sum % 11) { | |
case 1: | |
computedDv = 'k'; | |
break; | |
case 0: | |
computedDv = 0; | |
break; | |
default: | |
computedDv = 11 - (sum % 11); | |
break; | |
} | |
return computedDv.toString().toUpperCase() === cDv.toString().toUpperCase(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment