如何为每个空表单字段生成错误消息

f0brbegy  于 2021-09-23  发布在  Java
关注(0)|答案(5)|浏览(367)

我试图为每个空字段显示一条错误消息,我的问题是当我提交带有空字段(一个或两个)的表单时,所有错误消息都会出现。我希望每个空字段只显示一条错误消息,而不是全部。
html:

  1. <form action="" id="my-form">
  2. <label for="name">
  3. <input type="text" id="name" name="firstName" placeholder="First Name">
  4. <p class="error-field">First Name cannot be empty</p>
  5. </label>
  6. <label for="last-name">
  7. <input type="text" id="last-name" name="lastName" placeholder="Last Name">
  8. <p class="error-field">Last Name cannot be empty</p>
  9. </label>
  10. <label for="email">
  11. <input type="email" id="email" name="email" placeholder="Email Address">
  12. <p class="error-field">Looks like this is not an email</p>
  13. </label>
  14. <label for="password">
  15. <input type="password" id="password" name="password" placeholder="Password">
  16. <p class="error-field">Password cannot be empty</p>
  17. </label>
  18. <button type="submit" name="submit" class="form-button">Claim your free trial </button>
  19. <p>By clicking the button, you are agreeing to our <a href="" class="terms-conditions-link">Terms and Services</a></p>
  20. </form>

javascript:

  1. const submitButton = document.querySelector('.form-button');
  2. const errorField = document.querySelectorAll(".error-field");
  3. const validate = (e) => {
  4. e.preventDefault();
  5. const firstName = document.getElementById("name");
  6. const lastName = document.getElementById("last-name");
  7. const email = document.getElementById("email");
  8. const password = document.getElementById("password");
  9. if(firstName.value < 1 ) {
  10. errorField.forEach((f) => f.classList.toggle('error-active'));
  11. errorField.forEach((c) => c.style.color = "red");
  12. firstName.classList.toggle("invalid");
  13. return false;
  14. }
  15. if (lastName.value < 1) {
  16. errorField.forEach((f) => f.classList.toggle("error-active"));
  17. errorField.forEach((c) => (c.style.color = "red"));
  18. lastName.classList.toggle("invalid");
  19. return false;
  20. }
  21. if (email.value < 1) {
  22. errorField.forEach((f) => f.classList.toggle("error-active"));
  23. errorField.forEach((c) => (c.style.color = "red"));
  24. email.classList.toggle("invalid");
  25. return false;
  26. }
  27. if (password.value < 1) {
  28. errorField.forEach((f) => f.classList.toggle("error-active"));
  29. errorField.forEach((c) => (c.style.color = "red"));
  30. password.classList.toggle("invalid");
  31. return false;
  32. } else {
  33. password.classList.remove("invalid");
  34. errorField.classList.remove("error-active");
  35. }
  36. return true;
  37. }
  38. submitButton.addEventListener('click' , validate);
8wigbo56

8wigbo561#

您可以使用类作为输入,并跟踪 isValid 形式的布尔值。您正在使用代码设置所有错误字段。在这里,我们可以使用 closest() 寻找包容 label 那么 querySelector 查找错误字段的步骤

  1. el.closest('label').querySelector('.error-field');
  1. const submitButton = document.querySelector('.form-button');
  2. const validate = (e) => {
  3. e.preventDefault();
  4. let isValid = true
  5. document.querySelectorAll('.validate').forEach(el => {
  6. let error = el.closest('label').querySelector('.error-field').classList;
  7. if (el.value.trim().length === 0) {
  8. isValid = false;
  9. error.add('error-active');
  10. el.classList.add('invalid')
  11. } else {
  12. error.remove('error-active');
  13. el.classList.remove('invalid')
  14. }
  15. })
  16. return isValid;
  17. }
  18. submitButton.addEventListener('click', validate);
  1. .error-field.error-active,
  2. input.invalid{
  3. color: #f00;
  4. }
  1. <form action="" id="my-form">
  2. <label for="name">
  3. <input type="text" id="name" class='validate' name="firstName" placeholder="First Name">
  4. <p class="error-field">First Name cannot be empty</p>
  5. </label>
  6. <label for="last-name">
  7. <input type="text" id="last-name" class='validate' name="lastName" placeholder="Last Name">
  8. <p class="error-field">Last Name cannot be empty</p>
  9. </label>
  10. <label for="email">
  11. <input type="email" id="email" class='validate' name="email" placeholder="Email Address">
  12. <p class="error-field">Looks like this is not an email</p>
  13. </label>
  14. <label for="password">
  15. <input type="password" id="password" class='validate' name="password" placeholder="Password">
  16. <p class="error-field">Password cannot be empty</p>
  17. </label>
  18. <button type="submit" name="submit" class="form-button">Claim your free trial </button>
  19. <p>By clicking the button, you are agreeing to our <a href="" class="terms-conditions-link">Terms and Services</a></p>
  20. </form>
展开查看全部
mspsb9vt

mspsb9vt2#

这是因为在每个if语句中,您循环遍历表单中的所有错误字段,并将其全部更新。因此,您可以做的是,首先为html文件中的每个dom条目添加唯一id,例如err password、error name等,然后在每个if语句中获取需要显示错误的相关eror字段,并仅更新该字段。

c6ubokkw

c6ubokkw3#

使用nextelementsibling将大大简化您的代码。。。因为错误消息总是在 input .
在显示或不显示错误的条件下。。这就是问题所在 value.length 你必须检查一下。

  1. const submitButton = document.querySelector('.form-button');
  2. const errorField = document.querySelectorAll(".error-field");
  3. const validate = (e) => {
  4. // Remove any already displayed error
  5. errorField.forEach(function(error){
  6. error.classList.remove("invalid");
  7. })
  8. // Loop through all inputs to check the value length
  9. document.querySelectorAll("form input").forEach(function(input){
  10. if(input.value.length < 1){
  11. input.nextElementSibling.classList.toggle("invalid");
  12. }
  13. })
  14. // Prevent submit only if there are errors shown
  15. let errorCount = document.querySelectorAll(".error-field.invalid").length
  16. if(errorCount){
  17. e.preventDefault();
  18. }
  19. }
  20. submitButton.addEventListener('click' , validate);
  1. label{
  2. display: block;
  3. }
  4. label p{
  5. margin: 0;
  6. }
  7. .error-field{
  8. display: none;
  9. color: red;
  10. }
  11. .invalid{
  12. display: inline-block;
  13. }
  1. <form action="" id="my-form">
  2. <label for="name">
  3. <input type="text" id="name" name="firstName" placeholder="First Name">
  4. <p class="error-field">First Name cannot be empty</p>
  5. </label>
  6. <label for="last-name">
  7. <input type="text" id="last-name" name="lastName" placeholder="Last Name">
  8. <p class="error-field">Last Name cannot be empty</p>
  9. </label>
  10. <label for="email">
  11. <input type="email" id="email" name="email" placeholder="Email Address">
  12. <p class="error-field">Looks like this is not an email</p>
  13. </label>
  14. <label for="password">
  15. <input type="password" id="password" name="password" placeholder="Password">
  16. <p class="error-field">Password cannot be empty</p>
  17. </label>
  18. <button type="submit" name="submit" class="form-button">Claim your free trial </button>
  19. <p>By clicking the button, you are agreeing to our <a href="" class="terms-conditions-link">Terms and Services</a></p>
  20. </form>
展开查看全部
4uqofj5v

4uqofj5v4#

希望这能解决你的问题。注意,password改为passwordd,您访问所有错误字段时没有指定哪个字段

  1. const submitButton = document.querySelector('.form-button');
  2. const errorField = document.querySelectorAll(".error-field");
  3. const validate = (e) => {
  4. e.preventDefault();
  5. const firstName = document.getElementById("name");
  6. const lastName = document.getElementById("last-name");
  7. const email = document.getElementById("email");
  8. const passwordD = document.getElementById("password");
  9. if (firstName.value < 1) {
  10. errorField[0].classList.toggle('error-active');
  11. errorField[0].style.color = "red";
  12. firstName.classList.toggle("invalid");
  13. }
  14. if (lastName.value < 1) {
  15. errorField[1].classList.toggle("error-active");
  16. errorField[1].style.color = "red";
  17. lastName.classList.toggle("invalid");
  18. }
  19. if (email.value < 1) {
  20. errorField[2].classList.toggle("error-active");
  21. errorField[2].style.color = "red";
  22. email.classList.toggle("invalid");
  23. }
  24. if (password.value < 1) {
  25. errorField[3].classList.add("error-active");
  26. errorField[3].style.color = "red";
  27. passwordD.classList.toggle("invalid");
  28. } else {
  29. passwordD.classList.remove("invalid");
  30. errorField.forEach((f) => {
  31. f.classList.remove("error-active");
  32. f.style.color = "black";
  33. });
  34. return true;
  35. }
  36. return false;
  37. }
  38. submitButton.addEventListener('click', validate);
  1. <form action="" id="my-form">
  2. <label for="name">
  3. <input type="text" id="name" name="firstName" placeholder="First Name">
  4. <p class="error-field">First Name cannot be empty</p>
  5. </label>
  6. <label for="last-name">
  7. <input type="text" id="last-name" name="lastName" placeholder="Last Name">
  8. <p class="error-field">Last Name cannot be empty</p>
  9. </label>
  10. <label for="email">
  11. <input type="email" id="email" name="email" placeholder="Email Address">
  12. <p class="error-field">Looks like this is not an email</p>
  13. </label>
  14. <label for="password">
  15. <input type="password" id="password" name="password" placeholder="Password">
  16. <p class="error-field">Password cannot be empty</p>
  17. </label>
  18. <button type="submit" name="submit" class="form-button">Claim your free trial </button>
  19. <p>By clicking the button, you are agreeing to our <a href="" class="terms-conditions-link">Terms and Services</a></p>
  20. </form>
展开查看全部
sshcrbum

sshcrbum5#

我建议您使用一个表单验证js插件,而不是重新发明轮子,例如表单验证插件

相关问题