使用脚本验证电子邮件和表单条目

时间:2017-05-30 20:58:56

标签: javascript php

我有一个我已经使用了几年的脚本,它适用于我使用它的所有内容。在我使用它的新脚本上,它似乎突然被打破了。

我需要检查是否填写了First Name,并且输入的电子邮件地址仅为Gmail电子邮件地址。

我希望有人可以查看代码并告诉我是否遗漏了某些内容,或者告诉我更好的方法。

这是我的代码:

<form name="myForm" method="post" action="putform3.php" onsubmit="return validateForm()">
<div class='card-img-top' style="position: relative; width: 300px; padding: 5px; background-color: #989eae;"><center>
<b>Fill Out The Form Below</b><br />
<input class='card-img-top' name="fname" type="text" value="" style="width:280px; margin-bottom:5px; color:#000000;" placeholder="First Name" required /><br />
<input class='card-img-top' name="email" type="text" value="" style="width:280px; margin-bottom:5px; color:#000000;" placeholder="Gmail Email" required /><br />

<input type="hidden" name="affiliate" value="$affiliate" />
<input type="hidden" name="myip" value="$ip" />
<input type="hidden" name="lp" value="$lp" />
<input class="formbutton" type="submit" name="submit" value="$buttontext"><br />
<span style="font-size: 10px;">We keep your information private!</span>
</center></div>
</from>


<script type="text/javascript">
function validateForm()
{
var e=document.forms["myForm"]["email"].value;
var f=document.forms["myForm"]["fname"].value;
var atpos=e.indexOf("@");
var dotpos=e.lastIndexOf(".");
var gmail = e.split("@");
var rgmail = gmail[1];
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=e.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
if (rgmail == "yahoo.com" || rgmail == "hotmail.com" || rgmail == "msn.com" || rgmail == "aol.com" || rgmail == "yandex.com")
    {
  alert("Must Be A Good gmail.com Email Address");
  return false;
    }

if (f==null || f=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>  

javascript完全被忽略了。并不是因为它没有工作,而是它从未被使用过。我完全迷失在这里,因为它适用于我所有的其他脚本。我甚至在脚本中加了一个alert('worked');以确保它被使用并且它没有显示警报。

1 个答案:

答案 0 :(得分:1)

根据提供的网站链接:

website上有多个js问题导致js验证功能正常运行:

  
      
  1. 使用https:代替http进行jquery文件include url
  2.   {li>内script.js个文件window.getComputedStyle未定义   
  3. 删除验证函数中的其他lname值行           
  4. 形式没有这样的输入