-1
I created a form to send email, I can send the email but I can’t check if the fields are filled.
<script type="text/javascript">
function checkEmail () {
var name = document.form.name;
var email = document.form.email;
var subject = document.form.subject;
var message = document.form.message;
var count = 0;
var flagError = false;
var error="";
if (name == "") {
error += "O campo Nome deve ser preenchido.";
count = count + 1
flagError = true;
}
if (email == "") {
error += "O endereço de e-mail deve ser preenchido.";
count = count + 1
flagError = true;
}
if (subject == "") {
error += "O campo Assunto deve ser preenchido.";
count = count + 1
flagError = true;
}
if (message == "") {
error += "O campo Mensagem deve ser preenchido.";
count = count + 1
flagError = true;
}
if (count > 0 )
alert("Os seguintes erros foram encontrados:\n" + erro);
if (!flagError) {
var illegalChars = /(@.*@)|(@\.)|(@\-)|(@_)(\.@)|(\-@)|(\.\.)|(^\.)|(\.$)|(\.\-)|(\._)|(\-\.)|(_\.)|(^_)|(_$)|(_\-)|(\-\-)|(^\-)|(\-$)|(\-_)/;
if (email.match(illegalChars)) {
error += "O endereço de e-mail contém caracteres inválidos.";
count = count + 1
flagError = true;
}
}
if (!flagError) {
var emailFilter = /^\S+\@(\[?)[a-zA-Z0-9_\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (!(emailFilter.test(email))) {
error += "O endereço de e-mail não está em um formato válido.";
count = count + 1
flagError = true;
}
}
if (!flagError) {
var emailFilter = /^([a-zA-Z0-9\@_\-\.\+]+)$/;
if (!(emailFilter.test(email))) {
error += "O endereço de e-mail não está em um formato válido.";
count = count + 1
flagError = true;
}
}
if (!flagError) {
flagError = false;
window.alert("Mensagem enviada com sucesso!")
}
if (flagError) {
window.alert(error);
}
return !flagError;
}
</script>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Contato</title>
</head>
<body>
<p>Envie sua mensagem:</p>
<form method="post" action="http://www18.locaweb.com.br/scripts/FormMail.pl"/>
<p><input type="text" placeholder="Nome" required name="name" id="name"></p>
<p><input type="text" placeholder="Email" required name="email" id="email"></p>
<p><input type="text" placeholder="Assunto" required name="subject" id="subject"></p>
<p><input type="text" placeholder="Mensagem" required name="message" id="message"></p>
<p>
<button type="submit" name="Submit" onclick="javascript: var submit = checkEmail(getElementById('id').value); return submit;"> ENVIAR MENSAGEM </button>
</p>
</form>
</body>
</html>
What do you mean "does not check the fields"?
– RXSD
Does not check if the fields have been filled and does not check if the email is correct, and when all is right, does not appear the final email message sent successfully.
– M. Nunes
M. Nunes, managed to solve?
– RXSD
Yes, I managed to solve, I followed the guidance of Icaro Martins, it worked perfectly.
– M. Nunes