Last active
May 26, 2017 21:02
-
-
Save aatronco/379fa8bcd627fa288c0b420feaea23c2 to your computer and use it in GitHub Desktop.
Validate format and verification digit of Chile tax ID (RUT)
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
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$("#order_shipping_address_taxid").attr("pattern", "[0-9]{7,8}-[0-9Kk]{1}"); // with . and . [0-9]{1,2}.[0-9]{3}.[0-9]{3}-[0-9Kk]{1} | |
$("#order_shipping_address_taxid").attr("placeholder", "ej. 12345678-5"); | |
$("#order_shipping_address_taxid").attr("title", "ej. 12345678-5"); | |
var Fn = { | |
// Valida el rut con su cadena completa "XXXXXXXX-X" | |
validaRut : function (rutCompleto) { | |
if (!/^[0-9]+[-|‐]{1}[0-9kK]{1}$/.test( rutCompleto )) | |
return false; | |
var tmp = rutCompleto.split('-'); | |
var digv = tmp[1]; | |
var rut = tmp[0]; | |
if ( digv == 'K' ) digv = 'k' ; | |
return (Fn.dv(rut) == digv ); | |
}, | |
dv : function(T){ | |
var M=0,S=1; | |
for(;T;T=Math.floor(T/10)) | |
S=(S+T%10*(9-M++%6))%11; | |
return S?S-1:'k'; | |
} | |
} | |
$("#checkout").on("submit", function(){ | |
var tax_id = $("#order_shipping_address_taxid").val(); | |
if(!Fn.validaRut(tax_id)){ alert('RUT Inválido.'); } | |
return Fn.validaRut(tax_id); | |
}) | |
$("#checkout").on("submit", function(){ | |
var option = $("#order_other_boleta_o_factura option:selected").val(); | |
if(option == 'Factura Electronica'){ | |
if(!$('#order_other_razon_social').val() || !$('#order_other_giro').val()){ | |
alert('Razón Social o Giro no presente.'); | |
return false; | |
} | |
} | |
}) | |
$('#other_razon_social').hide(); // by default hidden | |
$('#other_giro').hide(); // by default hidden | |
$('#order_other_boleta_o_factura').on('change', function() { | |
var option = $("#order_other_boleta_o_factura option:selected").val(); | |
if(option == 'Factura Electronica'){ | |
console.log("Factura Electronica"); | |
$('#other_razon_social').show(); | |
$('#other_giro').show(); | |
}else{ | |
$('#other_razon_social').hide(); | |
$('#other_giro').hide(); | |
} | |
}) | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment