E
E
ElenaTih2020-11-09 15:09:40
Validation
ElenaTih, 2020-11-09 15:09:40

PHP mailer sends unvalidated messages, what to add?

I made my site according to the training course with ready-made js, php scripts. The php mailer form processes sending a message, but sends all messages, including those that did NOT pass validation. There is validation in js, it highlights lines in red if they are incorrectly filled.
function validateForms(form){
$(form).validate({
rules: {
name: {
required: true,
minlength: 2
},
phone: "required",
email: {
required: true,
email: true
}
},
messages: {
name: {
required: "Please enter your name",
minlength: jQuery.validator.format("Enter {0} characters!")
},
phone: "Please enter your phone number",
email: {
required: "Please , enter your email",
email: "Invalid email address"
}
}
});
}

validateForms('#consultation-form');
validateForms('#consultation form');
validateForms('#order form');

$('input[name=phone]').

But there is a message sending, if you click Send. What needs to be added to js, ​​what is the condition so that messages from the form that did not pass validation are not sent? Now function validateForms(form) { is }, then there seems to be no property where it is written to send only validated forms: $('form').submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST ",
url: "mailer/smart.php",
data: $(this).serialize()
}).done(function() {
$(this).find("input").val("");
$ ('#consultation, #order').fadeOut();
$('.overlay, #thanks').fadeIn('slow');

$('form' .trigger('reset');
});
return false;
});

And another question about captcha: if you put a ready-made Google captcha, do you need to register validation in js too?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anastasia, 2020-11-09
@nastya97core

You need to do a validation check:

$('form').submit(function(e) {
if (валидация === успех) {
$.ajax({
type: "POST",
url: "mailer/smart.php",
... 
} else {
появление ошибки, если валидация не пройдена
}
}

I just don't know how you know if the validation passed or not. Probably validateForms('#order form'); should return something. Try to write after and check the logs. Perhaps there is something $('form').submit(function(e) {console.log(validateForms('#order form'););

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question