hola a todos
me atrevo a pedirles ayuda ya que no he podido encontrarle una solucion al problema.
Tengo un codigo de validacion de rut(codigo de persona en Chile) y en distintos input tipo text, en el evento onblur, llamo a esta validacion.
El problema es que cuando este "rut" sea inválido que no salga de este input.
Gracias, les coloco el código.
Código HTML:
<input name="USUARIO" type="text" id="USUARIO" size="32" onblur="Javascript:ValidaRut(this.name);" >
Código HTML:
function revisarDigito( dvr )
{
dv = dvr + ""
if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k' && dv != 'K')
{
alert("Debe ingresar un digito verificador valido");
alert("2");
// window.document.form1.rut.focus();
// window.document.form1.rut.select();
return false;
}
return true;
}
function revisarDigito2( crut )
{
largo = crut.length;
if ( largo < 2 )
{
//alert(largo);
if (largo!==0) {
//alert(largo);
alert("Debe ingresar el rut completo")
// window.document.form1.rut.focus();
// window.document.form1.rut.select();
}
return false;
}
if ( largo > 2 )
rut = crut.substring(0, largo - 1);
else
rut = crut.charAt(0);
dv = crut.charAt(largo-1);
revisarDigito( dv );
if ( rut == null || dv == null )
return 0
var dvr = '0'
suma = 0
mul = 2
for (i= rut.length -1 ; i >= 0; i--)
{
suma = suma + rut.charAt(i) * mul
if (mul == 7)
mul = 2
else
mul++
}
res = suma % 11
if (res==1)
dvr = 'k'
else if (res==0)
dvr = '0'
else
{
dvi = 11-res
dvr = dvi + ""
}
if ( dvr != dv.toLowerCase() )
{
//alert("1");
alert("EL rut es incorrecto");
// window.document.form1.rut.focus();
// window.document.form1.rut.select();
return false
}
return true
}
function ValidaRut(ControlRUT)
{
//alert(ControlRUT);
// var texto = document.all[ControlRUT].value;
var texto = document.getElementById(ControlRUT).value;
var tmpstr = "";
for ( i=0; i < texto.length ; i++ )
if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' )
tmpstr = tmpstr + texto.charAt(i);
texto = tmpstr;
largo = texto.length;
if ( largo < 2 )
{
//alert(largo);
if (largo!==0) {
//alert(largo);
alert("Debe ingresar el rut completo");
// window.document.form1.rut.focus();
// window.document.form1.rut.select();
document.all[ControlRUT].value="";
}
return false;
}
for (i=0; i < largo ; i++ )
{
if ( texto.charAt(i) !="0" && texto.charAt(i) != "1" && texto.charAt(i) !="2" && texto.charAt(i) != "3" && texto.charAt(i) != "4" && texto.charAt(i) !="5" && texto.charAt(i) != "6" && texto.charAt(i) != "7" && texto.charAt(i) !="8" && texto.charAt(i) != "9" && texto.charAt(i) !="k" && texto.charAt(i) != "K" )
{
alert("El valor ingresado no corresponde a un R.U.T valido");
document.all[ControlRUT].value="";
// window.document.form1.rut.focus();
// window.document.form1.rut.select();
return false;
}
}
var invertido = "";
for ( i=(largo-1),j=0; i>=0; i--,j++ )
invertido = invertido + texto.charAt(i);
var dtexto = "";
dtexto = dtexto + invertido.charAt(0);
dtexto = dtexto + '-';
cnt = 0;
for ( i=1,j=2; i<largo; i++,j++ )
{
if ( cnt == 3 )
{
dtexto = dtexto + '.';
j++;
dtexto = dtexto + invertido.charAt(i);
cnt = 1;
}
else
{
dtexto = dtexto + invertido.charAt(i);
cnt++;
}
}
invertido = "";
for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ )
invertido = invertido + dtexto.charAt(i);
// document.all[ControlRUT].value = invertido.toUpperCase()
document.getElementById(ControlRUT).value = invertido.toUpperCase()
if ( revisarDigito2(texto) ){
alert("pasa por aca 2");
return true;
}else{
//document.getElementById(ControlRUT).focus();
}
//document.all[ControlRUT].value="";
return false;
}