Kelvin Halim Kelvin Halim - 6 months ago 21
Javascript Question

enabled button if password match

here is my script

$("#reg_confirm_pass").blur(function(){
var user_pass= $("#reg_pass").val();
var user_pass2=$("#reg_confirm_pass").val();
var enter = $("#enter").val();

if(user_pass.length == 0){
alert("please fill password first");
enter.disabled = true;
} else if (user_pass == user_pass2 ){
enter.disabled = false;
} else {
enter.disabled = true;
alert("Your password doesn't same");
}

});


this my html

Password : <input type="password" name="user[user_pass]" id="reg_pass" required="required">
Confirm password <input type="password" name="user[user_confirm_pass]" id="reg_confirm_pass" required="required">
<button type="submit" id="enter" disabled="true" value="Register">Register</button>


i am really new in Javascript and jQuery, and this is my first using jquery. i need to make a disabled button if the password doesn't match but, after i put the same password the button is still disabled.

Answer

$("#reg_confirm_pass").blur(function() {
  var user_pass = $("#reg_pass").val();
  var user_pass2 = $("#reg_confirm_pass").val();
  //var enter = $("#enter").val();

  if (user_pass.length == 0) {
    alert("please fill password first");
    $("#enter").prop('disabled',true)//use prop()
  } else if (user_pass == user_pass2) {
    $("#enter").prop('disabled',false)//use prop()
  } else {
    $("#enter").prop('disabled',true)//use prop()
    alert("Your password doesn't same");
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
Password :
<input type="password" name="user[user_pass]" id="reg_pass" required="required">Confirm password
<input type="password" name="user[user_confirm_pass]" id="reg_confirm_pass" required="required">
<button type="submit" id="enter" disabled="true" value="Register">Register</button>

Use .prop()

Comments