I'm have a JavaScript code to validate my contact form and do the POST to PHP script, the problem is that script ignors the IF statement and do the post while the response from my validateForm function is false.
If validateForm() returns false, it's still doing the POST action
My JS code:
$(document).ready(function() {
$("form[name='contactf']").submit(function(e) {
// do the extra stuff here
e.preventDefault();
if(validateForm()){
$.ajax({
type: "POST",
url: "mail.php",
data: $(this).serialize(),
success: function(data) {
if(data.message == 'success'){
M.toast({html: 'Mensagem enviada com sucesso!'})
$('#first_name').val('');
$('#last_name').val('');
$('#subject').val('');
$('#email').val('');
$('#phone').val('');
$('#details').val('');
}else if(data.message == 'error'){
M.toast({html: 'Ops... Tente novamente dentro de alguns minutos.'})
}
}
})
}
else
;
})
function validateForm()
{
var name=document.forms["contactf"]["first_name"].value;
var surname=document.forms["contactf"]["last_name"].value;
var subject=document.forms["contactf"]["subject"].value;
var mail=document.forms["contactf"]["email"].value;
var phone=document.forms["contactf"]["phone"].value;
var details=document.forms["contactf"]["details"].value;
var isnum = /^\d+$/.test(phone);
if(!isnum){
M.toast({html: 'O telefone deve conter apenas números!'});
return false;
}
else if (name==null || name=="",surname==null || surname=="",subject==null || subject=="",mail==null || mail=="",phone==null || phone=="",details==null || details=="")
{
M.toast({html: 'Preencha todos os campos!'})
return false;
}
else
return true;
}
function id( el ){
return document.getElementById( el );
}
})
Aucun commentaire:
Enregistrer un commentaire