Created
September 27, 2018 13:39
-
-
Save juanbrujo/ce6f0feeaebdc5498c68be3ca6a1ba8e to your computer and use it in GitHub Desktop.
validarNumTarjeta, checkRut, formatearRut π¨π±
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
function validarNumTarjeta(element, show){ | |
exvisa = /^4[0-9]{3,}$/g; | |
exmaster = /^5[1-9][0-9]{2,}$/g; | |
examerican = /^3[47][0-9]{2,}$/g; | |
exdinner= /^3(?:0[0-5]|[68][0-9])[0-9]{1,}$/g; | |
element.on('input', function(e){ | |
if (/\D/g.test(this.value)){ | |
this.value = this.value.replace(/\D/g, ''); | |
var text = this.value.replace(/\D/g, '').split(''); | |
if(text.length <= 16){ | |
var textoFinal =""; | |
$.each(text, function(index, value){ | |
if(index%4 === 0 && index>0){textoFinal+=" ";} | |
textoFinal+=value; | |
if(index === text.length-1){element.val(textoFinal);} | |
}); | |
}else{this.value = this.value.substr(0,16);} | |
}else{ | |
var cod = this.value.replace(/\D/g,''); | |
var text = this.value.replace(/\D/g,'').split(''); | |
if(text.length <= 16){ | |
var textoFinal =""; | |
$.each(text, function(index, value){ | |
if(text.length >= 4 && show){ | |
if(exvisa.test(cod)){$("#visa-card").show();} | |
if(exmaster.test(cod)){$("#master-card").show();} | |
if(exdinner.test(cod)){$("#dinners-card").show();} | |
if(examerican.test(cod)){$("#american-card").show();} | |
}else{ | |
$("#visa-card").hide(); | |
$("#american-card").hide(); | |
$("#master-card").hide(); | |
$("#dinners-card").hide(); | |
} | |
if(index%4 === 0 && index>0){textoFinal+=" ";} | |
textoFinal+=value; | |
if(index === text.length-1){element.val(textoFinal);} | |
}); | |
}else{this.value = this.value.substr(0,16);} | |
} | |
}); | |
} | |
function checkRut(rut) { | |
var valor = rut.replace(/[.]/g,''); | |
valor = valor.replace(/[-]/g,''); | |
cuerpo = valor.slice(0,-1); | |
dv = valor.slice(-1).toUpperCase(); | |
rut = cuerpo + '-'+ dv; | |
if(cuerpo.length < 7) {return false; } | |
suma = 0; | |
multiplo = 2; | |
for(i=1;i<=cuerpo.length;i++) { | |
index = multiplo * valor.charAt(cuerpo.length - i); | |
suma = suma + index; | |
if(multiplo < 7) { multiplo = multiplo + 1; } else { multiplo = 2; } | |
} | |
dvEsperado = 11 - (suma % 11); | |
dv = (dv == 'K')?10:dv; | |
dv = (dv == 0)?11:dv; | |
if(dvEsperado != dv) {return false; } | |
return true; | |
} | |
function formatearRut(element){ | |
element.on('input', function(e){ | |
var text = this.value.replace(/[.-]/g,''); | |
if(text[text.length-1].toUpperCase() === "K" && text[text.length-2].toUpperCase() === "K"){ text = text.substr(0,text.length-1); } | |
var textoFinal = ""; | |
$.each(text.split(''), function(index, value){ | |
textoFinal.replace(/[.-]/g,''); | |
if( !(/[0-9]/g).test(value) && value!=='k' && value!=='K'){ | |
value = ''; | |
}else{ | |
if(text.length<=8){ | |
if(index!=7 && (value!=='k' || value!=='K')){value= value.replace(/\D/g,'');} | |
if(index === 1){textoFinal+=".";} | |
if(index === 4){textoFinal+=".";} | |
if(index === 7){textoFinal+="-";} | |
textoFinal+=value; | |
}else{ | |
if(index!=8 && (value!=='k' || value!=='K')){value= value.replace(/\D/g,'');} | |
if(index === 2){textoFinal+=".";} | |
if(index === 5){textoFinal+=".";} | |
if(index === 8){textoFinal+="-";} | |
textoFinal+=value; | |
} | |
} | |
}); | |
this.value = textoFinal; | |
checkRut(this.value) ? this.setCustomValidity('') : this.setCustomValidity('Rut Invalido'); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment