3

我想将用户的电子邮件地址与确认的电子邮件地址相匹配。我已经尝试过 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>
4

2 回答 2

14

使用 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>
于 2013-08-14T05:23:46.567 回答
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>
于 2013-08-14T05:06:17.103 回答