I have a password field and a password confirmation field. I'm trying to add keyup to both these fields so that whenever someone starts typing in either, I can show them whether the passwords match or not.
I implemented a version of this which worked, but as you can see, it's not ideal.
function comparePassword() {
var password1 = $("#password1").val();
var password2 = $("#password2").val();
if (password1 != password2) {
$(".divDoPasswordsMatch").html("Passwords do not match!");
}
else {
$(".divDoPasswordsMatch").html("Passwords match.");
}
$("#password1, #password2").keyup(comparePassword);
I don't want to set the variables password1 and password2 inside the comparePassword function. This is in part because I need to use this function multiple times. I'd like to pass those two input fields into keyup to look something like this:
$("#password1, #password2").keyup(comparePassword($("#password1").val(), $("#password2").val()));
Ideally, I want comparePassword to know to inherit the values from #password1 and #password2 without me having to specify again:
$("#password1, #password2").keyup(comparePassword(this.value, this.value));
These last two code samples don't work. How should I write this?
Thanks in advance!