Structure of content on the web
Interfaces for building web applications
Scan a website for free
ValidityState
rangeOverflow
tooShort
BeforeUnloadEvent
HTMLAreaElement
HTMLBodyElement
HTMLDataElement
HTMLDocument
HTMLFormControlsCollection
HTMLFrameSetElement
Deprecated
HTMLHtmlElement
HTMLLIElement
HTMLMapElement
HTMLMeterElement
HTMLOptGroupElement
HTMLParagraphElement
HTMLQuoteElement
HTMLSpanElement
HTMLTableColElement
HTMLTemplateElement
HTMLTrackElement
HashChangeEvent
MessageChannel
PageRevealEvent
Plugin
Deprecated
PluginArray
Deprecated
Window
<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.
input:invalid {
outline: red solid 3px;
}
body {
margin: 0.5rem;
}
pre {
padding: 1rem;
height: 2rem;
background-color: lightgrey;
outline: 1px solid grey;
}
<pre id="log">Validation logged here...</pre>
<input type="text" id="userText" minlength="4" />
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…");
}
});
Specification |
---|
HTML # dom-validitystate-tooshort-dev |
BCD tables only load in the browser