I’ve been messing around with a validation like this for about a year now:
$(document).ready(function(){
$('#contact-form').submit(function(){
var name = $('#inputnimi').val(),
email = $('#inputemail').val(),
message = $('#inputmessage').val();
var nameerror = "<h4>Puuttuva kenttä</h4>",
emailerror = "<h4>Puuttuva kenttä</h4>",
messageerror= "<h4>Puuttuva kenttä</h4>";
if(name == ""){
$('.message-console').html(nameerror);
return false;
}
if(email == ""){
$('.message-console').html(emailerror);
return false;
}
if(message == ""){
$('.message-console').html(messageerror);
return false;
}
$.ajax({
type: "POST",
url: "contact.php",
data: form-data,
dataType: "text",
error: function(){alert("ERROR");
},
success: function() {
alert("success");
}
});
return false;
});
});
However, I don’t like this because:
- It checks errors one by one, which is annoying on long forms
- I know there’s a better way to do it
And I was wondering how I’m going to validate all the fields on one click, and all I could manage to think is something like this, which is obliviously never going to work:
if(name||email||message == ""){
$('.message-console').html(errors);
return false;
}
So I want to validate the form without any plugins, and show all errors at once, if there’s any. But how?
You can try something like this:
However if any of the values is equal to 0 the validation will be triggered again, so you better use