Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...