简体   繁体   English

未捕获的类型错误:无法在 Javascript 中读取 null 的属性“重置”

[英]Uncaught TypeError: Cannot read property 'reset' of null in Javascript

I am trying to validate email in a form input by a button click.我正在尝试通过单击按钮在表单输入中验证电子邮件。 Here is the code:这是代码:

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>

On clicking the Next button I get an alert message correctly but after that it redirects to next page instead of resetting the current one.单击“下一步”按钮时,我会正确收到一条警报消息,但之后它会重定向到下一页,而不是重置当前页面。 On inspecting element in chrome's console,I found this error: Uncaught TypeError: Cannot read property 'reset' of null在 chrome 的控制台中检查元素时,我发现了这个错误: Uncaught TypeError: Cannot read property 'reset' of null

Where am I wrong here ?我哪里错了?

Add id to your form:id添加到您的表单中:

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

or或者

Try:尝试:

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;
}
}

Apparently I didn't have a form id in显然我没有表单ID

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

Addition of a form id resolved the issue.添加表单 ID 解决了该问题。

暂无
暂无

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

相关问题 未被捕获的TypeError:无法在Phaser中读取null的属性“重置” - Uncaught TypeError: Cannot read property 'reset' of null in Phaser 未捕获的TypeError:无法读取未定义的属性“重置” - Uncaught TypeError: Cannot read property 'reset' of undefined Javascript未捕获的TypeError:无法读取null的属性&#39;getElementsByTagName&#39; - Javascript Uncaught TypeError: Cannot read property 'getElementsByTagName' of null Javascript:未捕获的TypeError:无法读取null的属性&#39;indexOf&#39; - Javascript: Uncaught TypeError: Cannot read property 'indexOf' of null JAVASCRIPT-未捕获的TypeError:无法读取null的属性“值” - JAVASCRIPT - Uncaught TypeError: Cannot read property 'value' of null 未捕获的类型错误无法读取 javascript 中 null 的属性(读取“值”) - Uncaught typeerror cannot read property of null (reading 'value') in javascript “未捕获的类型错误:无法读取空 JAVASCRIPT 的属性‘长度’ - "Uncaught TypeError: Cannot read property 'length' of null JAVASCRIPT Javascript控制台:未捕获的TypeError:在displaynone处无法读取null的属性“样式” - Javascript console: Uncaught TypeError: Cannot read property 'style' of null at displaynone JavaScript:未捕获的TypeError:无法读取null的属性&#39;appendChild&#39; - JavaScript: Uncaught TypeError: Cannot read property 'appendChild' of null 未捕获的TypeError:无法读取null javascript的属性&#39;appendChild&#39; - Uncaught TypeError: Cannot read property 'appendChild' of null javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM