我正在尝试通过单击按钮验证输入表单中的电子邮件。 这是代码:
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 的控制台中检查元素时,我发现了这个错误: Uncaught TypeError: Cannot read property 'reset' of null
我哪里错了?
最佳答案
添加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;
}
}
关于javascript - 未捕获的类型错误 : Cannot read property 'reset' of null in Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31063919/