Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<script>
var Fn = {
validaRut: function(rutCompleto) {
if (!/^[0-9]+-[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';
}
}
//1.- Poner placeholder:
$("#order_shipping_address_taxid").attr("placeholder", "xxxxxxxx-x (sin puntos)")
$("#order_billing_address_taxid").attr("placeholder", "xxxxxxxx-x (sin puntos)")
// 2.- Validar on Submit
$("#checkout").submit(function(evt) {
let sameAsBilling = $("#shipping_same_as_billing").is(":checked");
let rutShipping = $("#order_shipping_address_taxid").val();
let rutBilling = $("#order_billing_address_taxid").val();
if (sameAsBilling) {
if (Fn.validaRut(rutShipping)) {
return;
} else {
evt.preventDefault();
alert("Formato de RUT debe ser xxxxxxxx-x sin puntos");
}
} else {
console.log("validar dos rut:" + rutShipping + "&" + rutBilling);
if (Fn.validaRut(rutShipping)) {
return;
} else {
evt.preventDefault();
alert("Formato de RUT en dirección de envio debe ser xxxxxxxx-x sin puntos");
}
if (Fn.validaRut(rutBilling)) {
return;
} else {
evt.preventDefault();
alert("Formato de RUT en dirección de facturación debe ser xxxxxxxx-x sin puntos");
}
}
});
/* Optionally hide/show Giro Razon Social
$(document).ready(function(){
$('#other_razon_social').hide(); // by default hidden
$('#other_giro').hide(); // by default hidden
let option = $("#order_other_boleta_o_factura option:selected").val();
if(option == 'factura'){
$('#other_razon_social').show();
$('#other_giro').show();
$('#order_other_razon_social').attr("required","required");
$('#order_other_giro').attr("required","required");
}else{
$('#other_razon_social').hide();
$('#other_giro').hide();
$('#order_other_razon_social').removeAttr("required");
$('#order_other_giro').removeAttr("required");
}
});
$('#order_other_boleta_o_factura').on('change', function() {
let option = $("#order_other_boleta_o_factura option:selected").val();
if(option == 'factura'){
$('#other_razon_social').show();
$('#other_giro').show();
$('#order_other_razon_social').attr("required","required");
$('#order_other_giro').attr("required","required");
}else{
$('#other_razon_social').hide();
$('#other_giro').hide();
$('#order_other_razon_social').removeAttr("required");
$('#order_other_giro').removeAttr("required");
}
})
*/
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment