javascript - 如果验证失败则禁用提交按钮

标签 javascript html jquery css disable

如果下面的所有三个验证规则都失败,我想禁用我的提交按钮,否则禁用 false。任何帮助

<script>
  const form = document.getElementById('signup-form');
  let name = document.getElementById('name');
  let email = document.getElementById('email');
  let password = document.getElementById('password');
  let button = document.getElementById("signup-button");
  form.addEventListener('keyup', (e) => {
    e.preventDefault();
    checkValidation();
  });

  function checkValidation() {
    let nameValue = name.value.trim();
    let emailValue = email.value.trim();
    let passwordValue = password.value.trim();
    let emailValidate = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
    if (nameValue == "" || nameValue == null) {
      document.getElementById('name-error').style.display = 'block';
      document.getElementById('name-error').innerText = "Name Cannot be blank";
    } else {
      document.getElementById('name-error').style.display = 'none';
    }
    if (emailValue == "" || emailValue == null) {
      document.getElementById('email-error').style.display = 'block';
      document.getElementById('email-error').innerText = "Email Cannot be blank";
    } else if (!emailValidate.test(emailValue)) {
      document.getElementById('email-error').style.display = 'block';
      document.getElementById('email-error').innerText = "Please Enter a Valid email";
    } else {
      document.getElementById('email-error').style.display = 'none';

    }
    if (passwordValue == "" || passwordValue == null) {
      document.getElementById('password-error').style.display = 'block';
      document.getElementById('password-error').innerText = "Password Cannot be blank";

    } else {
      document.getElementById('password-error').style.display = 'none';
    }
  }
</script>

现在我想禁用我的提交按钮?如何实现

最佳答案

在该函数中,初始化一个变量,将 isValid 初始化为 true

在检查中,如果任何检查失败,请将 isValid 设置为 false

并在函数底部添加一个条件以启用或禁用“提交”按钮。我提供了示例代码供您引用。

if (isValid === true) {
  // Enable the submit button
}
else {
  // Enable the submit button
}

关于javascript - 如果验证失败则禁用提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72930175/

相关文章:

javascript - 使用 strophe.js 获取 SID 和 RID

javascript - 为什么这个广泛使用的脚本会杀死我的处理器

javascript - Laravel 请求 $request->file() 始终为空

html - 图像幻灯片的行为与 CSS 不符合预期

Javascript 替换不起作用?

javascript - Jquery 在点击类时附加按钮

php - PHP/jQuery Uncaught SyntaxError : Unexpected token ILLEGAL

javascript - reveal.js 中出现特定幻灯片时执行函数

javascript - 从另一个 iframe 中的链接在 iframe 中加载页面

<head> 中 <link> 元素的 Jquery 选择器