Validar rut (Chile) en formulario
Publicado por Felipe Vidal (1 intervención) el 14/08/2015 21:30:32
Hola amigos,
tengo un formulario con dos campos: rut y mail, actualmente tengo un script para validar que los campos no esten vacios y que el mail sea un mail valido y que me muestra los mensajes de error dentro de los campos
Pero no se como agregar el validador de rut valido. la mayoria de los ejemplos que he visto validan el rut con un archivo externo o con una funcion externa y no se como hacer para integrar eso a lo que ya tengo.
https://jsfiddle.net/nqopn077/
tengo un formulario con dos campos: rut y mail, actualmente tengo un script para validar que los campos no esten vacios y que el mail sea un mail valido y que me muestra los mensajes de error dentro de los campos
Pero no se como agregar el validador de rut valido. la mayoria de los ejemplos que he visto validan el rut con un archivo externo o con una funcion externa y no se como hacer para integrar eso a lo que ya tengo.
https://jsfiddle.net/nqopn077/
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Recipes 8.2, 8.3, and 8.4</title>
<script language="javascript" type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<style type="text/css">
body, input, textarea {
font-size:12px;
line-height:18px;
font-family:Verdana, Geneva, sans-serif;
}
input {width:200px;}
.submit {width:120px;}
#error {
color:red;
font-size:10px;
display:none;
}
.needsfilled {
background:red;
color:white;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
email = $("#email");
rut = $("#rut");
emptymail = "ingresa tu email.";
emptyrut = "ingresa tu rut.";
erroremail = "Email invalido.";
$("#theform").submit(function(){
if (rut.val().length < 1) {
rut.val(emptyrut);
rut.addClass("needsfilled");
return false;
}
if (email.val().length < 1) {
email.val(emptymail);
email.addClass("needsfilled");
return false;
}
else if (email.hasClass("needsfilled")) {
return false;
}
if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
email.val(erroremail);
email.addClass("needsfilled");
return false;
}
if ($(":input").hasClass("needsfilled")) {
return false;
}
});
$(":input").focus(function(){
if ($(this).hasClass("needsfilled") ) {
$(this).val("");
$(this).removeClass("needsfilled");
}
});
});
//Funcion formato rut//
function formato_rut()
{
var sRut1 = document.getElementById("rut").value;
sRut1=sRut1.replace('-', '');// se elimina el guion
sRut1=sRut1.replace('.', '');// se elimina el primer punto
sRut1=sRut1.replace('.', '');// se elimina el segundo punto
sRut1 = sRut1.replace(/k$/,"K");
document.getElementById("rut").value=sRut1;
//contador de para saber cuando insertar el . o la -
var nPos = 0;
//Guarda el rut invertido con los puntos y el guión agregado
var sInvertido = "";
//Guarda el resultado final del rut como debe ser
var sRut = "";
for(var i = sRut1.length - 1; i >= 0; i-- )
{
sInvertido += sRut1.charAt(i);
if (i == sRut1.length - 1 )
sInvertido += "-";
else if (nPos == 3)
{
sInvertido += ".";
nPos = 0;
}
nPos++;
}
for(var j = sInvertido.length - 1; j >= 0; j-- )
{
if (sInvertido.charAt(sInvertido.length - 1) != ".")
sRut += sInvertido.charAt(j);
else if (j != sInvertido.length - 1 )
sRut += sInvertido.charAt(j);
}
//Pasamos al campo el valor formateado
document.getElementById("rut").value = sRut.toUpperCase();
}
</script>
</head>
<body>
<form action="mail" id="theform" name="theform" method="post">
<label>rut:</label>
<input name="rut" id="rut" type="text" placeholder="11111111-1" maxlength="12" max="12">
<label>email:</label>
<input name="email" id="email" type="text" placeholder="nombre@mail.cl">
<input class="submit" type="submit" name="submit" value="Enviar" />
</form>
</body>
</html>
Valora esta pregunta


0