简体   繁体   English

JavaScript验证未激活,可能是由于HTML表单上的php mailer

[英]javascript validation not activating possibly due to php mailer on HTML form

I have a problem with my Javascript validation not activating. 我的Javascript验证未激活有问题。 I also have a PHP mailer on this HTML form and as soon as submit is pressed it submits the form and no validation is done. 我在此HTML表单上也有一个PHP邮件程序,一旦按下Submit,它将提交表单,并且没有完成验证。

Here is the code for the HTML form: 这是HTML表单的代码:

 <form name="contact" action="mailer.php" onsubmit="return ValidateForm()" method="post"><fieldset>

<tr><td><label for="name">Name:</label></td>
<td><input type="text" name="name" id="name" placeholder="Enter your full name" />
</td></tr>

<tr><td><label for="email">Email:</label></td>
<td><input type="email" name="email" id="email" placeholder="Enter your email address" />
</td></tr>

<tr><td><label for="tel">Phone:</label></td>
<td><input type="tel" name="tel" id="tel" placeholder="Enter your phone number" />
</td></tr>

<tr><td><label for="message">Message:</label></td>
<td><textarea id="message" name="message" placeholder=""></textarea>
</td></tr>

<tr><td><input type="submit" value="Submit" name="submit" />
</form>

Here is the code for the PHP mailer: 这是PHP邮件程序的代码:

<?php
if(isset($_POST['submit'])) {
$to = "email@address.com.au";
$subject = "Subject";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$tel_field = $_POST ['tel'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Phone: $tel_field\n Message:\n $message";


mail($to, $subject, $body, "From: $email_field");
echo "Data has been submitted to $to!";
echo "<a href='index.html'>Go back to Home Page</a>";
} else {
echo "error";
}
?>

Here is the code for the validator: 这是验证器的代码:

<script type="text/javascript">

{function validateForm()
{
var x=document.forms["contact"]["name"].value;
if (x==null || x=="")
  {
  alert("Name field must be filled out");
  return false;
  }

   var x=document.forms["contact"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }

 var x=document.forms["contact"]["tel"].value;
if (y==null || y=="")
  {
  alert("Phone number must be provided");
  return false;
  }

  var x=document.forms["contact"]["message"].value;
if (x==null || x=="")
  {
  alert("You have not entered a message");
  return false;
  }

  </script>

I have tried putting the javascript validator in as a separate file on the server or the script itself in the form html document and neither way seems to work. 我尝试将javascript验证器作为单独的文件放在服务器上,或者将脚本本身以html文档的形式放置,但似乎都无法正常工作。 I have done a lot of searching and have checked the code and am pretty sure it's right however could not find anything to say whether or not these two functions are possible together. 我做了很多搜索并检查了代码,很确定这是正确的,但是找不到任何东西可以说这两个功能是否可以同时使用。 If someone could please confirm that it would be awesome! 如果有人可以请确认那太好了!

Any advice would be appreciated! 任何意见,将不胜感激!

Thanks in advance! 提前致谢!

Kaz 卡兹

In your onSubmit you are calling return ValidateForm() when you should be doing return validateForm() with a lowercase "v". onSubmit ,当您应该使用小写的“ v” return validateForm()时,您将调用return ValidateForm() There are some others: 还有其他一些:

{function validateForm() should be function validateForm() {function validateForm()应该是function validateForm()

You never close the functions opening brace after the if statement. 在if语句后,您永远不要关闭打开括号的函数。

<form name="contact" action="mailer.php" onsubmit="return validateForm()" method="post">

and then: 接着:

function validateForm() {
    var x = document.forms["contact"]["name"].value;
    if (x == null || x == "") {
        alert("Name field must be filled out");
        return false;
    }

    var x = document.forms["contact"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
        alert("Not a valid e-mail address");
        return false;
    }

    var x = document.forms["contact"]["tel"].value;
    if (y == null || y == "") {
        alert("Phone number must be provided");
        return false;
    }

   var x = document.forms["contact"]["message"].value;
    if (x == null || x == "") {
        alert("You have not entered a message");
        return false;
    }
}

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

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