I've always found this plugin interesting because it leaves the form simple with subtle messages to the visitor. I decided to implement and fall into the following dilemma:
How do I make the return of my AJAX activate some warning on my form? for example:
I have a simple input with the user name, with min-lenght of 2. If I put 1 character only, it will activate and warn that it needs to have 2, but in case I need to make an ajax request to my server and check if this name already exists in my database?
If it exists, it proceeds normal, if not, it shows the error (same as min-lenght)
My current code:
$(document).ready(function(){
var validator = $("#contactform").validate({
rules: {
name: {
required: true,
minlength: 2
}
},
messages: {
name: {
required: "Preencha o nome",
minlength: "Minimo: 2 caracter"
}
}
submitHandler: function(form) {
$("#contactform").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "form.php",
data: $(this).serialize(),
success: function() {
if (result.status == "OK") {
// aqui está tudo certo e aparece um aviso
}else{
// ESSA É A DÚVIDA: caso o nome exista, ele ativa o input com a mensagem de retorno
}
}
});
return false;
});
},
});
});