<p>
<label for="age">Your (21 to 65) </label>
<input type="number" name="age" required id="age" min="21" max="65" />
</p>
<p>
<button type="button" id="check">checkValidity()</button>
</p>
<pre id="log"></pre>HTMLInputElement: checkValidity() method / notebook
HTMLInputElement: checkValidity() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const output = document.querySelector("#log");
const checkButton = document.querySelector("#check");
const ageInput = document.querySelector("#age");
ageInput.addEventListener("invalid", () => {
output.textContent += "Invalid event fired.\n";
});
checkButton.addEventListener("click", () => {
const checkVal = ageInput.checkValidity();
output.textContent = `checkValidity returned: ${checkVal}`;
});