JS Form Validation
Give a field a visible label and a useful error message. Validate the value before showing a successful result.
Where to run this
This example changes a document. Open it in the HTML playground, then select Run code. It runs inside a separate preview.
A practical example
<label for="email">Email</label>
<input id="email" type="email" required>
<button id="check" type="button">Check email</button>
<p id="result" role="status"></p>
<script>
document.getElementById("check").addEventListener("click", () => {
const input = document.getElementById("email");
document.getElementById("result").textContent = input.checkValidity() ? "Ready to review" : "Enter a valid email address";
});
</script>
What happens
The message explains whether the browser accepts the current email field.