Foros del Web » Programando para Internet » Jquery »

Validar rut (Chile) en formulario

Estas en el tema de Validar rut (Chile) en formulario en el foro de Jquery en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 14/08/2015, 13:27
 
Fecha de Ingreso: diciembre-2008
Mensajes: 3
Antigüedad: 15 años, 4 meses
Puntos: 0
Validar rut (Chile) en formulario

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.

[URL="https://jsfiddle.net/nqopn077/"]https://jsfiddle.net/nqopn077/[/URL]

Código:
<!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&oacute;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="[email protected]">
    <input class="submit" type="submit" name="submit" value="Enviar" />
</form>

</body>
</html>

Etiquetas: chile, formulario, funcion, html, input, javascript, js, rut, valor
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 21:22.