我在网站上有一个表单,用户需要输入他们的名字并通过电子邮件两次下载。但是,用户可以在字段中键入两个不同的电子邮件,点击提交,它仍将显示成功消息。为什么?我可以使用模式属性来完成此任务吗?我希望用户被强制输入两次相同的电子邮件或收到错误消息。
这是我的HTML:
<form id="form" method="post" action="formmail.php" name="form" width="100%">
<input type="hidden" name="good_url" value="https://MYURL.com/index.html#submitgood" />
<input type="hidden" name="bad_url" value="https://MYURL.com/index.html#submitbad" />
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT,AUTH_TYPE,REMOTE_USER" />
<input type="hidden" name="derive_fields" value="email=EmailAddr,realname=username" />
<input type="hidden" name="recipients" value="myaddress" />
<input type="hidden" name="subject" value="Download" />
<fieldset>
<legend>Please fill out the information below to download.<br><br>Filesize, 59.8 MB.</legend><br>
<table cellspacing="0" cellpadding="0" id="confquest"><tr height="80px"><td>
<label for="Name" id="namelabel"><strong>Full name:</strong></label><br>
<input id="Name" type="text" name="username" title="Enter your full name" placeholder="Your Name" autofocus required /></td></tr>
<tr height="80px"><td><label for="eMail" id="emaillabel"><strong>Email address:</strong></label><br>
<input id="eMail" type="email" name="EmailAddr" title="Enter your email address" placeholder="example@mail.com" required /></td></tr>
<tr height="80px"><td><label for"eMail_repeat" id="emaillabel2"><strong>Repeat Email address:</strong></label><br>
<input id="eMail_repeat" type="email" name="email_addr_repeat" title="Repeat your email address" placeholder="example@mail.com" required oninput="check(this)" /></td></tr>
</table>
<input id="reset2" type="reset" name="reset" value="Clear" />
<input id="submit2" type="submit" name="submit" value="Submit" />
<input type="hidden" name="mail_options"
value="HTMLTemplate=https://www.MYURL.com/fmtemplates/mailtemplate5.html" />
</fieldset></form>
答案 0 :(得分:1)
在电子邮件重复的输入字段之后添加此内容后,它会起作用。
<script>
function check(input) {
if (input.value != document.getElementById('eMail').value) {
input.setCustomValidity('The two email addresses must match.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('');
}
}
</script>