• 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. <input>, <fieldset> or text input element. A constraint has been added using the minlength attribute so the input expects a string with a minimum of 4 characters. If the user enters a string that's too short, the element fails constraint validation, and the styles matching :invalid CSS pseudo-class are applied.

      css
      input:invalid {
        outline: red solid 3px;
      }
      
      html
      <pre id="log">Validation logged here...</pre>
      <input type="text" id="userText" minlength="4" />
      
      js
      const userInput = document.getElementById("userText");
      const logElement = document.getElementById("log");
      
      function log(text) {
        logElement.innerText = text;
      }
      
      userInput.addEventListener("input", () => {
        userInput.reportValidity();
        if (userInput.validity.tooShort) {
          log("Not enough characters entered.");
        } else {
          log("Input is valid…");
        }
      });
      

      Specifications

      Specification
      HTML
      # dom-validitystate-tooshort-dev

      Browser compatibility

      BCD tables only load in the browser

      See also