首页 > 解决方案 > 完成必填字段后,如何允许提交表单?

问题描述

我已经完成了带有即时字段验证的代码,但如果字段没有值,我也想阻止提交表单。我已经设法阻止提交表单,但是当我返回并提供一个值时,它仍然不允许它提交。知道我在这里做错了什么吗?

这是HTML:

<form id="contactForm" action="https://formspree.io/xrggbejj" method="POST" onsubmit="return formValidation(contactForm);">

  <div class="row">

  <div class="col">
    <label for="fName">First Name</label>
  </div>

  <div class="col">
    <input type="text" id="fName" name="firstname" onblur="checkFirstName(firstName)" autofocus/>
    <img src="../images/alert-icon.png" class="alert" id="firstNameMsg" alt="alert icon" width="40" height="35"/>
  </div>
  </div> <!-- End of row -->

  <div class="row">

  <div class="col">
    <label for="lName">Last Name</label>
  </div>

  <div class="col">
    <input type="text" id="lName" name="lastname" onblur="checkLastName(lastName)"/>
    <img src="../images/alert-icon.png" class="alert" id="lastNameMsg" alt="alert icon" width="40" height="35"/>
  </div>
  </div> <!-- End of row -->

  <div class="row">

  <div class="col">
    <label for="subject">Questions and/or Comments</label>
  </div>

  <div class="col">
    <textarea id="subject" name="subject" onblur="checkSubject(subject)" style="height:200px"></textarea>
    <img src="../images/alert-icon.png" class="alert" id="subjectMsg" alt="alert icon" width="40" height="35"/>
  </div>
  </div> <!-- End of row -->

  <div class="row">
    <input type="submit" value="SEND" />
  </div> <!-- End of row -->
</form>

这是JavaScript:

联系表单字段验证

var alertStyle;
var alertImg;
const firstName = document.forms['contactForm']['fName'];
const lastName = document.forms['contactForm']['lName'];
const subject = document.forms['contactForm']['subject'];

function checkFirstName(firstName) {

  if (firstName.value === '') {
    alertStyle = document.getElementById('fName').style.border = '2px solid rgba(255, 6, 0)';
    alertImg = document.getElementById('firstNameMsg').style.display = 'inline-block';
    return false;
  } else if (firstName.value !== '') {
    alertStyle = document.getElementById('fName').style.border = '1px solid #ccc';
   alertImg = document.getElementById('firstNameMsg').style.display = 'none';
   return true;
 }
}

function checkLastName(lastName) {

  if (lastName.value === '') {
    alertStyle = document.getElementById('lName').style.border = '2px solid rgba(255, 6, 0)';
    alertImg = document.getElementById('lastNameMsg').style.display = 'inline-block';
    return false;
  } else if (lastName.value !== '') {
    alertStyle = document.getElementById('lName').style.border = '1px solid #ccc';
    alertImg = document.getElementById('lastNameMsg').style.display = 'none';
    return true;
 }
}

function checkSubject(subject) {

  if (subject.value === '') {
    alertStyle = document.getElementById('subject').style.border = '2px solid rgba(255, 6, 0)';
    alertImg = document.getElementById('subjectMsg').style.display = 'inline-block';
    return false;
  } else if (firstName.value !== '') {
    alertStyle = document.getElementById('subject').style.border = '1px solid #ccc';
    alertImg = document.getElementById('subjectMsg').style.display = 'none';
    return true;
}}

提交时联系表单验证

function formValidation(contactForm) {

  if (firstName && lastName && subject !== true) {
    alert('Please fill out required fields.')
    event.preventDefault()
    return false;
  } else if (firstName && lastName && subject === true) {
    return true;
 }
}

标签: javascriptformsvalidationonsubmit

解决方案


firstName和变量指向输入而不是它们内部的实际值lastNamesubject您可以将您的功能修改为

    function formValidation(contactForm) {

      if (!firstName.value || !lastName.value || !subject.value) {
        alert('Please fill out required fields.')
        event.preventDefault()
        return false;
      } else {
        return true;
      }
    }

在这里,我使用!运算符将​​输入转换为何True时为空,如果任何输入为空,则第一种情况将评估,否则您的函数将返回 true。


推荐阅读