1

I'm currently trying to set up a registration page on my website but I have a problem: My JS script does not act like I want it to do

Here is my script:

function checkPasswordMatch() { var password = $("#password").val(); var confirmPassword = $("#retypepassword").val(); if (password != confirmPassword) { $("#pass").html("Passwords do not match!"); $("#submit").attr("disabled"); } else { $("#pass").html("Password match."); $("#submit").removeAttr("disabled"); } } $(document).ready(function () { $("#retypepassword").keyup(checkPasswordMatch); }); 

And my html code:

<INPUT class="btn btn-primary" type="submit" id="submit" name="" value="Register" disabled /> 

So in my opinion it's supposed to add the "disabled" attribute when passwords do not match and remove it when they match but here is what happen :

When the passwords match, the disabled state is removed but if they do not match (if they are modified for any reason for instance) they state does not come back again

2 Answers 2

1

Like AlexGm, I suggest you try this.

function checkPasswordMatch() {	var password = $("#password").val();	var confirmPassword = $("#retypepassword").val();	// Note: !==	var noMatch = password !== confirmPassword;	if (noMatch) {	$("#pass").html("Passwords do not match!");	} else {	$("#pass").html("Password match.");	}	$("#submit").attr("disabled", noMatch); }

Sign up to request clarification or add additional context in comments.

1 Comment

Shorter and easier than what I tried to do plus it works, thank you!
1

To achieve what you describe, try changing it slightly to:

$("#submit").attr("disabled", true);

and

$("#submit").attr("disabled", false);

1 Comment

Thank you very much, I do appreciate your help, it worked like expected!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.