bhylu.com

Advertisements

API Validation

The constraint validation API checks HTML rules such as required and min. Use checkValidity to inspect the current state.

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

HTML + JavaScript

<label for="count">Places</label>
<input id="count" type="number" min="1" max="6" value="0">
<button id="check" type="button">Check</button>
<p id="result"></p>
<script>
  document.getElementById("check").addEventListener("click", () => {
    const valid = document.getElementById("count").checkValidity();
    document.getElementById("result").textContent = valid ? "Valid count" : "Choose 1 to 6";
  });
</script>

What happens

Zero fails the minimum rule; a value from one to six passes.