我想将用户的电子邮件地址与确认的电子邮件地址相匹配。 我已经尝试过HTML5验证,但它不起作用,我不知道为什么。我想在我的联系表单中使用jQuery来匹配。
<form action="contact.php" method="post" name="form1" id="form1">
<input name="email" type="text" required="1" id="email" size="44" />
<input name="emailConfirm" type="text" size="44" class="required email" equalTo='#email' equals="email" err="Confirm email must be the same as email" />
</form>
答案 0 :(得分:9)
使用javascript
<form action="contact.php" method="post" name="form1" id="form1">
<input name="email" type="text" required="1" id="email"/>
<input name="emailConfirm" type="text" id="confemail" onblur="confirmEmail()"/>
</form>
JS
<script type="text/javascript">
function confirmEmail() {
var email = document.getElementById("email").value
var confemail = document.getElementById("confemail").value
if(email != confemail) {
alert('Email Not Matching!');
}
}
</script>
答案 1 :(得分:2)
试试这个
<div class="required">
<label for="email">email</label><input type='email' id='p1'><br><br>
<label for="email">Confirm email</label><input type='email'
onfocus="validateMail(document.getElementById('p1'), this);"
oninput="validateMail(document.getElementById('p1'), this);">
</div>
Javascript
<script>
function validateMail(p1, p2) {
if (p1.value != p2.value || p1.value == '' || p2.value == '') {
p2.setCustomValidity('email incorrect');
} else {
p2.setCustomValidity('');
}
}
</script>