[英]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.