Last active
December 26, 2015 08:49
-
-
Save ricardosiri68/7125430 to your computer and use it in GitHub Desktop.
validation and ajax call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="" method="POST" id="regForm"> | |
<input type="text" name="nombre" placeholder="Nombre" id="nombre"> | |
<input type="text" name="apellido" placeholder="Apellido" id="apellido"> | |
<input type="text" name="us" placeholder="Usuario" id="us"> | |
<input type="password" name="pas" placeholder="Contraseña" id="pas"> | |
<input type="email" name="mail" placeholder="Email" id="mail" > | |
<input type="button" value="Registrarse" href='#' class="button" onclick="javascript: registro(3)"> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var conexion; | |
function crearXMLHttpRequest() | |
{ | |
var xmlHttp=null; | |
if (window.ActiveXObject) | |
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); | |
else | |
if (window.XMLHttpRequest) | |
xmlHttp = new XMLHttpRequest(); | |
return xmlHttp; | |
} | |
function highLightInput(input){ | |
return function(){ | |
input.style.border='1px solid #ccc'; | |
} | |
} | |
function showFormWarnning(input,fieldName){ | |
var detalles = document.getElementById("resp"); | |
detalles.innerHTML = "<span class='fail'><p>Por favor completa el campo "+fieldName+".</p></span>"; | |
input.focus(); | |
input.style.border="1px solid #E30022"; | |
setTimeout(highLightInput(input), 1000); | |
} | |
function validateField(objField){ | |
if(objField.input.value.length == 0){ | |
showFormWarnning(objField.input, objField.fieldName); | |
return false; | |
} | |
return true; | |
} | |
function registro(act){ | |
// | |
// Validar que todos lo campos esten completos | |
var form = document.getElementById("regForm"); | |
var fields = [ | |
{ input: form.nombre, fieldName: "Nombre"}, | |
{ input: form.apellido, fieldName: "Apellido"}, | |
{ input: form.us, fieldName: "Usuario"}, | |
{ input: form.pas, fieldName: "Contraseña" }, | |
{ input: form.mail, fieldName: "Email"} | |
]; | |
if(fields.every(validateField)) | |
{ | |
conexion=crearXMLHttpRequest(); | |
conexion.onreadystatechange = procesarEventos; | |
conexion.open('POST', 'ajax.php', true); | |
conexion.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); | |
conexion.send( | |
'nombre='+ form.nombre.value+ | |
'&apellido='+ form.apellido.value+ | |
'&user='+ form.us.value+ | |
'&email='+ form.mail.value+ | |
'&pass='+ form.pas.value+ | |
'&act='+ act); | |
} | |
} | |
function procesarEventos() | |
{ | |
var detalles = document.getElementById("resp"); | |
if(conexion.readyState == 4) | |
{ | |
resp = JSON.parse(conexion.responseText); | |
registro= resp.registro; | |
msg= resp.msg; | |
campo = resp.campo; | |
if (registro == 'si') { //Si no hay errores, se procede a avisar del registro correcto | |
detalles.innerHTML = msg; | |
document.getElementById("regForm").reset(); | |
}else{ | |
if(campo == 'user'){ //Si el usuario esta registrado | |
detalles.innerHTML = msg; | |
document.getElementById("regForm").us.focus(); | |
document.getElementById("regForm").us.style.border="1px solid #E30022"; | |
setTimeout("document.getElementById('regForm').us.style.border='1px solid #ccc'", 1000); | |
}else if (campo == 'email'){ // Si el email ya esta registrado, se envia el mensaje | |
detalles.innerHTML = msg; | |
document.getElementById("regForm").mail.focus(); | |
document.getElementById("regForm").mail.style.border="1px solid #E30022"; | |
setTimeout("document.getElementById('regForm').mail.style.border='1px solid #ccc'", 1000); | |
}else{ | |
detalles.innerHTML = msg; | |
} | |
} | |
} | |
else | |
{ | |
detalles.innerHTML = '<img src="img/loading.gif">'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment