I'm building a web application as some practice and I've designed the signup process so that you have to type your mail into an input and also confirm it by typing it twice in another input.
I want the verification process to be done as the user starts typing, not after pressing the sign-up button.
Below is the solution I came up with, but it has some issues: it checks if the values match only after finishing typing the #confirmMail
input (because that's what I told it to), but if the #confirmMail
input is correct and the user fixes only the #mail
input without typing anything more in the #confirmMail
, the error message still appears.
What is a better solution than the one below?
$("#confirmMail").blur
(
function () {
let mail1 = $("#mail")
let mail2 = $("#confirmMail")
if (!(mail1.val() === mail2.val())) {
document.getElementById("match").style.display=""
mail1.css({borderColor: "red",
borderWidth: "3px",
borderStyle:"solid"})
mail2.css({borderColor: "red",
borderWidth: "3px",
borderStyle:"solid"})
}
else if (mail1.val()=== mail2.val()){
document.getElementById("match").style.display="none"
mail1.css({borderWidth : "0px"})
mail2.css({borderWidth : "0px"})
}
}
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<input id="mail" type="text">
<input id="confirmMail" type = "text">
<span id="match"; style = "display: none; color:red" >Emails do not match</span>
<input id="submitInput" class="input" type="submit" value="SIGN-UP">
</div>