• Web APIs
  • ValidityState
  • Instance properties
    1. rangeOverflow
    2. tooShort
    3. BeforeUnloadEvent
    4. HTMLAreaElement
    5. HTMLBodyElement
    6. HTMLDataElement
    7. HTMLDocument
    8. HTMLFormControlsCollection
    9. HTMLFrameSetElement Deprecated
    10. HTMLHtmlElement
    11. HTMLLIElement
    12. HTMLMapElement
    13. HTMLMeterElement
    14. HTMLOptGroupElement
    15. HTMLParagraphElement
    16. HTMLQuoteElement
    17. HTMLSpanElement
    18. HTMLTableColElement
    19. HTMLTemplateElement
    20. HTMLTrackElement
    21. HashChangeEvent
    22. MessageChannel
    23. PageRevealEvent
    24. Plugin Deprecated
    25. PluginArray Deprecated
    26. Window
    27. required control, such as an <textarea>, has an empty value.

      If the required attribute is set, and no numeric input element. Constraints have been added using the required attribute which disallows empty values. If the user enters any value that's not a number greater than 17, the element fails constraint validation, and the styles matching :invalid are applied.

      css
      input:invalid {
        outline: red solid 3px;
      }
      
      html
      <pre id="log">Validation logged here...</pre>
      <input type="number" id="age" min="18" required />
      
      js
      const userInput = document.getElementById("age");
      const logElement = document.getElementById("log");
      
      function log(text) {
        logElement.innerText = text;
      }
      
      userInput.addEventListener("input", () => {
        userInput.reportValidity();
        if (userInput.validity.valid) {
          log("Input OK…");
        } else if (userInput.validity.valueMissing) {
          log("Required field cannot be empty.");
        } else {
          log("Bad input detected: " + userInput.validationMessage);
        }
      });
      

      Specifications

      Specification
      HTML
      # dom-validitystate-valuemissing-dev

      Browser compatibility

      BCD tables only load in the browser

      See also