Santiago Cuartas Rmrz
Santiago Cuartas Rmrz

Reputation: 171

Enable/Disable button based on password input length with Javascript

Trying to enable/disable a form button based on password input length but my code doesn't work. If the length is less than 15 characters button should be disabled.

So far my simple html form

<form class="form" id="form1" method="POST" action="submit.php" accept-charset="UTF-8" autocomplete="off">
   <input type="password" placeholder="Password" id="passfield">
   <button type="submit" id="form-submit">CONNECT</button>
</form>

No username field needed in my case

and so far my Javascript code

function submit_btn() {

  var field = document.getElementById("passfield");
  var button = document.getElementById("form-submit");

  if (field.value.lenght < 15){
    button.setAttribute("disabled", "");
  }else{
    button.removeAttribute("disabled");
  }
};

Upvotes: 0

Views: 909

Answers (1)

mplungjan
mplungjan

Reputation: 178126

Where do you call submit_btn?

Also spelling length

Here is a better version

const field = document.getElementById("passfield");
const button = document.getElementById("form-submit");

field.addEventListener("input", function() {
  button.disabled = field.value.length < 15;
})
<form class="form" id="form1" method="POST" action="submit.php" accept-charset="UTF-8" autocomplete="off">
  <input type="password" placeholder="Password" id="passfield">
  <button type="submit" disabled id="form-submit">CONNECT</button>
</form>

Upvotes: 1

Related Questions