Reputation: 2185
I have some form with jquery validantion, and i want to display all errors in same div
element above the form.
i try do this, but is not working:
$("#contactForm").validate({
rules: {
assunto: { required: true },
nome: { required: true, minlength: 2 },
email: { required: true, email: true },
telefone: { required: true },
mensagem: { required: true }
},
errorPlacement: function(error, element) {
if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem" )
//here i think i want to insert the location but i don't know how!
else
//here i think i want to insert the location but i don't know how!
},
messages: {
assunto: {required: 'É necessário escolher um assunto'},
nome: { required: 'É necessário preencher o campo nome', minlength: 'No mínimo 2 letras' },
email: { required: 'Informe seu email', email: 'Informe um Email válido' },
telefone: { required: 'Informe seu Telefone' },
mensagem: { required: 'Escreva sua Mensagem' }
},
submitHandler: function(form){
var dados = $( form ).serialize();
$.ajax({
type: "POST",
url: "enviafaleconosco.asp",
data: dados,
success: function( data )
{
alert("Obrigado! Seu contato foi enviado com sucesso. Breve retornaremos!");
$("#contactForm")[0].reset();
}
});
return false;
}
});
what i'm doing wrong?
Upvotes: 1
Views: 551
Reputation: 37167
You have a Syntax error here:
errorPlacement: function(error, element) {
if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem" )
//here i think i want to insert the location but i don't know how!
else
//here i think i want to insert the location but i don't know how!
},
If the if and else don't have nothing inside you must add the curly brackets {}
to close them:
errorPlacement: function(error, element) {
if (element.attr("name") == "assunto" || element.attr("name") == "nome" || element.attr("name") == "email" || element.attr("name") == "telefone" || element.attr("name") == "mensagem" ){
//here i think i want to insert the location but i don't know how!
} else {
//here i think i want to insert the location but i don't know how!
}
},
It is also a good practice to always add the curly brackets to if blocks, even if they are single line
Upvotes: 2