Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

validation - re-enter email validator not working on javascript

So I'm trying to check if the user inputs the same email address and password twice in a signup form using javascript. It shouldn't let them sign up if they don't match, however only the portion of the passwords is working and the email isn't working. Here's my code:

<form>
           
    <input type="email" name="fname" placeholder="Email Adress" required="required" class="input-txt" id="email1">
    <input type="email" name="fname" placeholder="Confirm Email Adress" required="required" class="input-txt" id="email2"> 
    <br><br>
    <input id="password" type="password" name="fname" placeholder="Create Password" required="required" class="input-txt">
    <input id="confirm_password" type="password" name="fname" placeholder="Confirm Password" required="required" class="input-txt"><br>
    <button type="submit" class="btn">Sign up</button>
</form>

<script>
function validateMail() {
    if(first_email.value != second_email.value) {
        email2.setCustomValidity("Emails Don't Match");
    } else {
        email2.setCustomValidity('');
    }
}
email1.onchange = validateMail;
email2.onkeyup = validateMail;

var password = document.getElementById("password")
  , confirm_password = document.getElementById("confirm_password");

function validatePassword(){
    if(password.value != confirm_password.value) {
        confirm_password.setCustomValidity("Passwords Don't Match");
    } else {
        confirm_password.setCustomValidity('');
    }
}

password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;
</script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to assign var in top of the script:

 var first_email = document.getElementById("email1")
  , second_email = document.getElementById("email2")
  , password = document.getElementById("password")
  , confirm_password = document.getElementById("confirm_password");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...