How to check whether the fields are filled or or?

  • 0
    There is a code for checking if the INPUT field is complete
    function validateForm() {
      // This function deals with validation of the form fields
      var x, y, i, valid = true;
      x = document.getElementsByClassName("tab");
      y = x[currentTab].getElementsByTagName("input");
      // A loop that checks every input field in the current tab:
      for (i = 0; i < y.length; i++) {
        // If a field is empty...
        if (y[i].value == "") {
          // add an "invalid" class to the field:
          y[i].className += " invalid";
          // and set the current valid status to false:
          valid = false;

    This code checks all the fields, but I have only 2 fields and I want one of them to be filled, or one or the other. Please help me with a solution
    JavaScript Anonymous, May 28, 2020

  • 1 Answers
  • 0
    Something like this:

    if (!y[0].value && !y[1].value) {
    // ... Первые два поля не заполнены ...

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!