- ValidityState valid properties.
- Constraint validation
- Forms: Data form validation
- Regular Expressions
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
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.
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="number" id="age" min="18" required />
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);
}
});
Specification |
---|
HTML # dom-validitystate-valuemissing-dev |
BCD tables only load in the browser