<input type="number" step="2" min="0" max="100" required />ARIA: aria-invalid attribute / notebook
ARIA: aria-invalid attribute
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<ul>
<li>
<label for="name">Full Name</label>
<input
type="text"
name="name"
id="name"
aria-required="true"
aria-invalid="false" />
</li>
<li>
<label for="email">Email Address</label>
<input
type="email"
name="email"
id="email"
aria-required="true"
aria-invalid="false" />
</li>
</ul>document.getElementById("name").addEventListener("blur", () => {
checkValidity(
"name",
" ",
"Invalid name entered (requires both first and last name)",
);
});
document.getElementById("email").addEventListener("blur", () => {
checkValidity("email", "@", "Invalid email address");
});function checkValidity(id, searchTerm, msg) {
const elem = document.getElementById(id);
if (elem.value.includes(searchTerm)) {
elem.setAttribute("aria-invalid", "false");
updateAlert();
} else {
elem.setAttribute("aria-invalid", "true");
updateAlert(msg);
}
}function updateAlert(msg) {
const oldAlert = document.getElementById("alert");
if (oldAlert) {
oldAlert.remove();
}
if (msg) {
const newAlert = document.createElement("div");
newAlert.setAttribute("role", "alert");
newAlert.setAttribute("id", "alert");
const content = document.createTextNode(msg);
newAlert.appendChild(content);
document.body.appendChild(newAlert);
}
}