简体   繁体   English

JavaScript表单验证。 如果需要字段,则发送表单事件

[英]JavaScript form validation. Forms sent event if fields are required

I got this form somebody else made. 我得到了别人制作的这张表格。 It's a really simple form. 这是一种非常简单的形式。 Problem is, the client is receiving empty submits. 问题是,客户正在接收空的提交。 The form already has some validation on it so I'm wondering how can it be submitted. 该表单已经有一些验证,因此我想知道如何提交。

 function check_message() { var check = true; if (document.getElementById('email').value == '') { check = false; document.getElementById('email').style.border = '3px solid red'; } else { document.getElementById('email').style.border = '1px solid #cccccc'; } if (document.getElementById('telephone').value == '') { check = false; document.getElementById('telephone').style.border = '3px solid red'; } else { document.getElementById('telephone').style.border = '1px solid #cccccc'; } if (check) { document.getElementById('thaForm').submit(); } } 
 <form id="thatForm" name="" action="soumission_process.php" method="post" class="wpcf7-form" novalidate> <input type="text" id="email" name="email" value="" size="40" value=""> <input type="text" id="telephone" name="telephone" value="" size="40" value=""> <input type="button" value="send" onClick="check_message();"> </form> 

In the soumission_process.php, it's just : 在soumission_process.php中,它只是:

$email= $_POST['email'];
$telephone = $_POST['telephone'];

Then mail the stuff... 然后把东西寄出去...

How could somebody submit the form skipping the validation ? 有人如何提交跳过验证的表格? I though maybe the form is just filled with blank space than submitted but there's a required SELECT and it's empty too. 我虽然表单可能只是用空白而不是提交的空格填充,但是有一个必需的SELECT且它也是空的。 If I try myself, I can't submit without filling all the fields and selecting an option in that select. 如果我自己尝试,则无法不填写所有字段并在该选择中选择一个选项而提交。

Help me with that weird Wordpress saved as html site :D 帮我处理保存为html站点的奇怪Wordpress:D

Check form datas in the php script, ex : 检查php脚本中的表单数据,例如:

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) && (isset($_POST['telephone']) && $_POST['telephone'] != '')){
  $email= $_POST['email'];
  $telephone = $_POST['telephone'];
  // mail datas
}else{
  // do nothing or display error
}

Solution 1 Add required to each field - this will not stop bots from harvesting and submitting post requests to the action 解决方案1在每个字段中添加必填字段-这不会阻止机器人获取并向操作提交发布请求

Solution 2 解决方案2

Remove the action and add it if all is ok 删除操作,如果一切正常,请添加它

In BOTH cases check on the server 在两种情况下,请检查服务器

 window.addEventListener("load", function() { document.getElementById("thatForm").addEventListener("submit", function(e) { var allOk = 0; var emailField = document.getElementById('email'); var ok = emailField.value.trim() !== '' ? 1 : 0; allOk += ok; emailField.classList.toggle("error", !ok) var telephoneField = document.getElementById('telephone') ok = telephoneField.value.trim() !== '' ? 1 : 0; allOk += ok; telephoneField.classList.toggle("error", !ok) if (allOk !== 2) { e.preventDefault(); } this.action = "soumission_process.php" }); }); 
 .error { border: 3px solid red } 
 <form id="thatForm" method="post" class="wpcf7-form" novalidate> <input type="text" id="email" name="email" value="" size="40" value=""> <input type="text" id="telephone" name="telephone" value="" size="40" value=""> <input type="submit" value="send"> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM