根据密码输入长度启用/禁用按钮使用JavaScript

发布于 2025-02-09 08:01:54 字数 670 浏览 1 评论 0原文

尝试根据密码输入长度启用/禁用表单按钮,但我的代码不起作用。如果长度小于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" id="form-submit">CONNECT</button>
</form>

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");
  }
};

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");
  }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2025-02-16 08:01:54

您在哪里致电submit_btn?

另外拼写长度

是更好的版本

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>

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文