HTML表单使用JS验证电子邮件,验证有效但表单仍然提交
我正在尝试验证使用 php 将表单信息发送到一组电子邮件的表单的电子邮件字段。我正在使用 Javascript 来验证电子邮件并且它正在工作。
问题是,即使在验证返回 false 后,表单仍然处理信息并发送电子邮件,我希望得到一些指导,因为我找不到我正在犯的错误。
这是我用于表单的 HTML 代码:
<form id="formulario-submit" onsubmit="return Validacion();" action="procesarsolicitud.php" method="POST">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input type="text" value="Nombre*" id="nombre" name="nombre" onclick='this.value = "";'>
</td>
</tr>
<tr>
<td>
<input type="text" value="Apellido*" id="apellido" name="apellido" onclick='this.value = "";'>
</td>
</tr>
<tr>
<td>
<!--<input type="submit" value="Enviar" id="enviar" alt="enviar">-->
<button name="enviar" value="Submit" type="submit" id="enviar" alt="enviar" width="100">
这是我用来验证电子邮件的 JS 代码:
<script>
function Validacion()
{
var x=document.forms["formulario-submit"]["email"].value;
var arroba=x.indexOf("@");
var punto=x.lastIndexOf(".");
if (arroba<1 || punto<arroba+2 || punto+2>=x.length)
{
alert("Not a valid Email.");
email.focus();
return false;
}
}
</script>
这是我用来处理数据并发送电子邮件的 PHP 代码(位于名为 procesarsolicitud.php 的单独 PHP 文件中) :
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$email = $_POST['email'];
$message = '
<html>
<head>
<title></title>
</head>
<body>
<h3>Form Data::</h3>
<tr>
<td width="200"><b>Nombre:</b></td>
<td>'.$nombre.'</td>
</tr>
<tr>
<td width="200"><b>Apellido:</b></td>
<td>'.$apellido.'</td>
</tr>
<tr>
<td width="200"><b>Email:</b></td>
<td>'.$email.'</td>
</tr>
<tr>
</tr>
</table><br />
</body>
</html>
';
// multiple recipients
$to='[email protected],[email protected]';
// subject
$subject = ' Email Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n".
'From: Email <[email protected]>' . "\r\n";
echo "MessageExample";
// Mail it
mail($to, $subject, $message, $headers);
提前致谢。
I'm trying to validate the email field of a form that uses php to send form information to a set of emails. I'm using Javascript to validate the email and it is working.
The problem is, even after the validation returns false the form still processes the information and sends the emails, i would love some guidance as I can't find the error i'm making.
Here's the HTML code i'm using for the form:
<form id="formulario-submit" onsubmit="return Validacion();" action="procesarsolicitud.php" method="POST">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input type="text" value="Nombre*" id="nombre" name="nombre" onclick='this.value = "";'>
</td>
</tr>
<tr>
<td>
<input type="text" value="Apellido*" id="apellido" name="apellido" onclick='this.value = "";'>
</td>
</tr>
<tr>
<td>
<!--<input type="submit" value="Enviar" id="enviar" alt="enviar">-->
<button name="enviar" value="Submit" type="submit" id="enviar" alt="enviar" width="100">
Here's the JS code i'm using to validate the email:
<script>
function Validacion()
{
var x=document.forms["formulario-submit"]["email"].value;
var arroba=x.indexOf("@");
var punto=x.lastIndexOf(".");
if (arroba<1 || punto<arroba+2 || punto+2>=x.length)
{
alert("Not a valid Email.");
email.focus();
return false;
}
}
</script>
Here's the PHP code (on a separate PHP file called procesarsolicitud.php) i'm using to process the data and send the email:
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$email = $_POST['email'];
$message = '
<html>
<head>
<title></title>
</head>
<body>
<h3>Form Data::</h3>
<tr>
<td width="200"><b>Nombre:</b></td>
<td>'.$nombre.'</td>
</tr>
<tr>
<td width="200"><b>Apellido:</b></td>
<td>'.$apellido.'</td>
</tr>
<tr>
<td width="200"><b>Email:</b></td>
<td>'.$email.'</td>
</tr>
<tr>
</tr>
</table><br />
</body>
</html>
';
// multiple recipients
$to='[email protected],[email protected]';
// subject
$subject = ' Email Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n".
'From: Email <[email protected]>' . "\r\n";
echo "MessageExample";
// Mail it
mail($to, $subject, $message, $headers);
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先你不应该使用 JS 来验证表单。准确地说..如果你这样做,你还必须检查输入服务器端(你也可以使用 AJAX 调用来验证服务器端的表单,然后你可以重用相同的清理程序/验证程序)。
关于你的具体问题,SOF“提问”不是搜索引擎。在提问之前,您应该花一些时间进行查看。您的问题已被提出(例如,谷歌的第一个结果“onsubmit form return false”http: //webdeveloper.com/forum/showthread.php?t=175736;来自 SOF onSubmit 返回 false 不起作用)多次。
ps 检查控制台(请参阅 Firebug 了解更多信息)是否有任何错误。
You shouldn't use JS to validate form in the first place. To be precise.. if you are doing that, you have to check the input server side as well (you could also use AJAX call to validate form on server-side, then you could reuse the same sanitizers/validators).
Regarding your specific issue, SOF "Ask Question" is not a search engine. You should invest some time looking before asking questions. Your question has been asked (e.g., first result from google "onsubmit form return false" http://webdeveloper.com/forum/showthread.php?t=175736; first result from SOF onSubmit returning false is not working) numerous times.
p.s. Check Console (ref. to Firebug for more info) if there are any errors.