JavaScript - validacion campo onblur

 
Vista:
Imágen de perfil de Cristian
Val: 3
Ha aumentado su posición en 12 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

validacion campo onblur

Publicado por Cristian (2 intervenciones) el 17/10/2018 21:53:41
Estoy validando un rut de un formulario por medio de onblur, la validacion la efectua correctamente, pero al desplegar mensaje de error y precionar aceptar debiera volver al campo. Sin embargo se queda ahi la ventana de alert.

Debo cambiarme a otra pestaña del navegador, volver y ahi si se cierra el alert al dar aceptar.

El JavaScript es:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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 1");
		window.document.form_ingreso.rut.focus();
		window.document.form_ingreso.rut.select();
		return false;
	}
	return true;
}
 
function revisarDigito2( crut )
{
	largo = crut.length;
	if ( largo < 2 )
	{
		alert("Debe ingresar el rut completo 2")
		window.document.form_ingreso.rut.focus();
		//window.document.form_ingreso.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("EL rut es incorrecto 3")
		window.document.form_ingreso.rut.focus();
		//window.document.form_ingreso.rut.select();		
		return false
	}
 
	return true
}
 
function Rut(texto)
{
	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("Debe ingresar el rut completo 4")
		window.document.form_ingreso.rut.focus();
		//window.document.form1_ingreso.rut.select();		
		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 5");
			window.document.form_ingreso.rut.focus();
			//window.document.form1_ingreso.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++ )
	{
		//alert("i=[" + i + "] j=[" + 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);
 
	window.document.form_ingreso.rut.value = invertido.toUpperCase()
 
	if ( revisarDigito2(texto) )
		return true;
 
	return false;
}

Y el formulario es:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="es">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
	<title>Untitled Document</title>
	<link href="/css/plantra1.css" rel="stylesheet" type="text/css" />
  <script src="/js/validarut.js"></script>
</head>
 
<body>
	<div id="wrapper" class="container">
    <h2>Formulario de Registro</h2>
 
    <form id="form_ingreso" class="" name="form_ingreso" action="#">
    	<fieldset>
        <div class="form-group">
        	<div>
          	<label class="control-label" for="rut">RUT</label>
            <input name="rut" id="rut" class="form-control" placeholder="99999999-9" type="text" onblur="Rut(document.form_ingreso.rut.value)" required="required">
          </div>
        </div>
 
 
        <div class="form-group">
        	<div>
          	<button type="button" class="btn btn-success btn-lg btn-block info">Send</button>
          </div>
        </div>
     	</fieldset>
    </form>
</div>
<!--<script src="/js/validarut.js"></script>-->
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 183
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

validacion campo onblur

Publicado por Yamil Bracho (78 intervenciones) el 17/10/2018 22:12:57
Si ya estas usando Jquery es mas facil que hagas

$('#rut').focus();

Tambien chequea si el alert es sincrono, porque puede ser que contien ejecutando el script sin esperar. Puedes usar JQueryUi para mostrar mensajes de error mas vistosos o usar BlockUI
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de ScriptShow
Val: 2.019
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

validacion campo onblur

Publicado por ScriptShow (692 intervenciones) el 20/10/2018 12:30:29
Saludos Cristian,

prueba esta opción:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="es">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<link href="/css/plantra1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
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 1");
	document.getElementById('rut').focus();
	document.getElementById('rut').select();
	return false;
	}
	return true;
}
 
function revisarDigito2( crut )
{
	largo = crut.length;
	if ( largo < 2 )
	{
	alert("Debe ingresar el rut completo 2");
	document.getElementById('rut').focus();
	//window.document.form_ingreso.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("EL rut es incorrecto 3");
	document.getElementById('rut').focus();
	//window.document.form_ingreso.rut.select();
	return false;
	}
	return true;
}
 
function Rut(texto)
{
	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("Debe ingresar el rut completo 4")
	document.getElementById('rut').focus();
	//window.document.form1_ingreso.rut.select();
	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 5");
	document.getElementById('rut').focus();
	//window.document.form1_ingreso.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++ )
	{
	//alert("i=[" + i + "] j=[" + 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.form_ingreso.rut.value = invertido.toUpperCase()
 
	if ( revisarDigito2(texto) )
	return true;
	return false;
}
</script>
</head>
<body>
<div id="wrapper" class="container">
    <h2>Formulario de Registro</h2>
    <form id="form_ingreso" class="" name="form_ingreso" action="#">
    <fieldset>
    <div class="form-group">
    <div>
    <label class="control-label" for="rut">RUT</label>
    <input name="rut" id="rut" class="form-control" placeholder="99999999-9" type="text" onblur="Rut(document.form_ingreso.rut.value)" required="required">
    </div>
    </div>
    <div class="form-group">
    <div>
    <button type="button" class="btn btn-success btn-lg btn-block info">Send</button>
    </div>
    </div>
    </fieldset>
    </form>
</div>
<!-- <script type="text/javascript" src="/js/validarut.js"></script> -->
</body>
</html>

Espero sea útil.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Cristian
Val: 3
Ha aumentado su posición en 12 puestos en JavaScript (en relación al último mes)
Gráfica de JavaScript

validacion campo onblur

Publicado por Cristian (2 intervenciones) el 20/10/2018 16:09:22
Me sigue pasando lo mismo, al verificar y mostrar alert por error en rut, le doy aceptar al alert, pero se mantiene y no me deja hacer nada mas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de ScriptShow
Val: 2.019
Plata
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

validacion campo onblur

Publicado por ScriptShow (692 intervenciones) el 20/10/2018 21:15:05
A ver si avanzamos en algo con:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="es">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<link href="/css/plantra1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
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 1");
	document.getElementById('rut').focus();
	document.getElementById('rut').select();
	return false;
	}
	return true;
}
 
function Rut(texto)
{
	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("Debe ingresar el rut completo 4")
	document.getElementById('rut').focus();
	//window.document.form1_ingreso.rut.select();
	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 5");
	document.getElementById('rut').focus();
	//window.document.form1_ingreso.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++ )
	{
	//alert("i=[" + i + "] j=[" + 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.getElementById('rut').value = invertido.toUpperCase()
        }
	if ( revisarDigito(texto) )
        {
	return true;
	return false;
        }
}
</script>
</head>
<body>
<div id="wrapper" class="container">
    <h2>Formulario de Registro</h2>
    <form id="form_ingreso" class="" name="form_ingreso" action="#">
    <fieldset>
    <div class="form-group">
    <div>
    <label class="control-label" for="rut">RUT</label>
    <input name="rut" id="rut" class="form-control" placeholder="99999999-9" type="text" onblur="Rut(document.form_ingreso.rut.value)" required="required">
    </div>
    </div>
    <div class="form-group">
    <div>
    <button type="button" class="btn btn-success btn-lg btn-block info">Send</button>
    </div>
    </div>
    </fieldset>
    </form>
</div>
<!-- <script src="/js/validarut.js"></script> -->
</body>
</html>

Un saludo
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar