Bien, ya he arreglado la validación on submit. El problema es que ahora, cuando lo escribo todo correctamente, no me salta ningún error, pero tampoco va a la acción determinada del formulario para insertar los datos en la base de datos.
Al final del todo hago la comprobación on submit.
Código Javascript
:
Ver original$(document).ready(function(){
//Variables globales
var usr = $("#username");
var pass = $("#password");
var cpass = $("#confirm_password");
var email = $("#email");
//Username Check
function validateUsername(){
usr = $("#username").val();
if(usr.length >= 4)
{
$("#status").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "check.php",
data: "username="+ usr,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){
if(msg == 'OK')
{
$("#username").removeClass('object_error'); // if necessary
$("#username").addClass("object_ok");
$(this).html(' <img src="tick.ico" align="absmiddle">');
return true;
}
else
{
$("#username").removeClass('object_ok'); // if necessary
$("#username").addClass("object_error");
$(this).html(msg);
return false;
}
});
}
});
}else{
$("#status").html('<font color="red"><br /><center>The username should have at least <strong>4</strong> characters.</center></font>');
$("#username").removeClass('object_ok'); // if necessary
$("#username").addClass("object_error");
return false;
}
}
//Password check
function validatePassword() {
pass = $("#password").val();
if(pass.length >= 6){
$("#status2").html(' <img src="tick.ico" align="absmiddle">');
return true;
}else{
$("#status2").html('<font color="red"><br /><center>The password should have at least <strong>6</strong> characters.</center></font>');
$("#password").removeClass('object_ok'); // if necessary
$("#password").addClass("object_error");
return false;
}
}
//Password confimation check
function validatePassword2() {
cpass = $("#confirm_password").val();
var pass = $("#password").val();
if(cpass == pass){
if(cpass.length >= 6){
$("#status3").html(' <img src="tick.ico" align="absmiddle">');
return true;
}else{
$("#status3").html('<font color="red"><br /><center>The password must have at least <strong>6</strong> characters!</center></font>');
$("#confirm_password").removeClass('object_ok'); // if necessary
$("#confirm_password").addClass("object_error");
return false;
}
}else{
$("#status3").html('<font color="red"><br /><center>The passwords<strong> doesn`t match!</center></strong></font>');
$("#confirm_password").removeClass('object_ok'); // if necessary
$("#confirm_password").addClass("object_error");
return false;
}
}
function validateEmail() {
email = $("#email").val();
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(email.match(mailformat)){
$("#status4").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "checkmail.php",
data: "email="+ email,
success: function(msg){
$("#status4").ajaxComplete(function(event, request, settings){
if(msg == 'OK')
{
$("#email").removeClass('object_error'); // if necessary
$("#email").addClass("object_ok");
$(this).html(' <img src="tick.ico" align="absmiddle">');
return true;
}
else
{
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
$(this).html(msg);
return false;
}
});
}
});
}else{
$("#status4").html('<font color="red"><br /><center>The email <strong>is invalid!</center></strong></font>');
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
return false;
}
}
//Validate checkbox
function validateCheck() {
if(document.form.agree.checked == true){
$("#status7").html('');
$("#agree").removeClass('object_error'); // if necessary
$("#agree").addClass("object_ok");
return true;
}else{
$("#status7").html('<font color="red">You must accept the<strong> agreement!</strong></font>');
$("#agree").removeClass('object_ok'); // if necessary
$("#agree").addClass("object_error");
return false;
}
}
//Validation on blur.
usr.blur(validateUsername);
pass.blur(validatePassword);
cpass.blur(validatePassword2);
email.blur(validateEmail);
//Validation on submit.
// Envio de formulario
$("#signupForm").submit(function(){
if(validateUsername() & validatePassword() & validatePassword2() & validateEmail() & validateCheck()){
return true;
}
else{
alert("adios");
return false;
}
});
});
¿Está bien así?
EDITO: Me he dado cuenta, de que siempre devuelve "false", aunque todo este correcto en el formulario. ¿Por qué podría ser?