未捕获的类型错误:无法读取 Javascript 中 null 的属性'reset'



我正在尝试验证通过单击按钮输入的表单中的电子邮件。这是代码:

function ValidateEmail(inputText) {
  var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
  if (inputText.value.match(mailformat)) {
    document.form1.coemail.focus();
    return true;
  } else {
    alert('You have entered an invalid email address!Enter again');
    document.getElementById('form1').reset();
    return false;
  }
}
<button class="btn btn-primary nextBtn btn-lg pull-right" type="button"onClick="ValidateEmail(document.form1.coemail)">Next</button>

单击"下一页"按钮后,我会正确地收到一条警告消息,但之后它会重定向到下一页,而不是重置当前页面。在检查chrome控制台中的元素时,我发现了以下错误:未捕获类型错误:无法读取null的属性"reset"

我哪里错了?

id添加到表单:

<form id="form1" role="form" name="form1" action="reg_employer.php" method="post">

尝试:

function ValidateEmail(inputText)
{
  var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
  if(inputText.value.match(mailformat))
{
  document.form1.coemail.focus();
  return true;
}
else
{
  alert("You have entered an invalid email address!Enter again");
  document.form1.reset();   //<== change this line, use form name
  return false;
}
}

显然我在中没有表单id

<form id="form1" role="form" name="form1" action="reg_employer.php" method="post">

添加表单id解决了此问题。

最新更新