Last active
February 27, 2021 18:33
-
-
Save matiaslopezd/08968f8421aa88c1ba1b7e631e348836 to your computer and use it in GitHub Desktop.
Chilean DNI validator and get in JSON { run: Number/String, adv: Number/String, dv: Number/String, valid: Boolean }
This file contains hidden or 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
| function DNI_CL({ run, string }){ | |
| if(run !== '' && (typeof run === "string" || typeof run === "number")){ | |
| function format(n){ | |
| if (typeof n === 'string') { | |
| // Remove '.' if exist | |
| n = (n.includes('.')) ? n.split('.').join('') : n; | |
| // Remove '-' if exist | |
| n = (n.includes('-')) ? n.split('-').join('') : n; | |
| } | |
| return n; | |
| } | |
| function DV(n){ | |
| // Initialize multiples and total | |
| let m = 2, sum = 0; | |
| // Reverse string | |
| n = n.split('').reverse().join(''); | |
| // Algorithm to get the DV | |
| n.split('').map((i) => { | |
| // Multiply digit by multiples [2,7] | |
| sum += (Number(i) * m); | |
| // Add one to mutiple variable | |
| m = (m < 7) ? m+1 : 2; | |
| }); | |
| // Get remainder of the division and after get subtraction | |
| const dv = (11 - (sum % 11)); | |
| return (dv === 10) ? 'K' : (dv === 11) ? 0 : dv ; | |
| } | |
| function formatter({run, dv}){ | |
| // Get the first numbers | |
| const a1 = run.slice(0, run.length - 6); | |
| // Get the last 6 numbrs | |
| const a2 = run.slice(run.length - 6, run.length).match(/.{1,3}/g).join('.'); | |
| // Return in string or number format | |
| return (string) ? ((dv) ? `${a1}.${a2}-${DV(run)}` : `${a1}.${a2}`) : ((dv && typeof DV(run) === "number") ? Number(`${run}${DV(run)}`) : Number(run)); | |
| } | |
| run = format(run); | |
| // Get DV | |
| let dv = run.toString().split('')[run.toString().split('').length - 1]; | |
| // If is number convert or set to uppercase | |
| dv = (Number(dv)) ? Number(dv) : dv.toUpperCase(); | |
| // Format and remove the last number | |
| run = run.toString().slice(0, -1); | |
| // Return JSON format | |
| return { | |
| run: formatter({run, dv: true}), | |
| adv: formatter({run}), | |
| dv: (string) ? DV(run).toString() : DV(run), | |
| valid: (DV(run).toString() === dv.toString()) | |
| } | |
| } else { | |
| return { run: 0, adv: 0, dv: 0, valid: false }; | |
| } | |
| } |
This file contains hidden or 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
| function DNI_CL({run,string}){if(run!==''&&(typeof run==="string"||typeof run==="number")){function format(n){if(typeof n==='string'){n=(n.includes('.'))?n.split('.').join(''):n;n=(n.includes('-'))?n.split('-').join(''):n} | |
| return n} | |
| function DV(n){let m=2,sum=0;n=n.split('').reverse().join('');n.split('').map((i)=>{sum+=(Number(i)*m);m=(m<7)?m+1:2});const dv=(11-(sum%11));return(dv===10)?'K':(dv===11)?0:dv} | |
| function formatter({run,dv}){const a1=run.slice(0,run.length-6);const a2=run.slice(run.length-6,run.length).match(/.{1,3}/g).join('.');return(string)?((dv)?`${a1}.${a2}-${DV(run)}`:`${a1}.${a2}`):((dv&&typeof DV(run)==="number")?Number(`${run}${DV(run)}`):Number(run))} | |
| run=format(run);let dv=run.toString().split('')[run.toString().split('').length-1];dv=(Number(dv))?Number(dv):dv.toUpperCase();run=run.toString().slice(0,-1);return{run:formatter({run,dv:!0}),adv:formatter({run}),dv:(string)?DV(run).toString():DV(run),valid:(DV(run).toString()===dv.toString())}}else{return{run:0,adv:0,dv:0,valid:!1}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo / Ejemplo
Codepen demo
How to use
This is a very simple function for get the validation of Chilean DNI. For use only need configurate
{ run }key that is the DNI and can be instringornumber, also can choose output DNI instringornumberformat in{ string } key.The output returns
runDNI formated based instringkey configuration (true/false),avdare the numbers before verifier digit,dvis the verifier digit and finallyvalidisbooleanindicate is valid or not.Como usar
Esta es una función muy simple para validar el RUT/RUN. Para empezar necesitamos configurar
{ run }que es el RUT/RUN y puede ser enstringonumber, también es posible configurar la salida del RUT enstringonumberen{ string }.La salida se compone de
runque es el RUT/RUN formateado basado en la keystring,avdson los números anteriores al dígito verificador,dves el dígito verificador y finalmentevalides unbooleanque indica la validez.