Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:valid — data validity selector

The CSS `:valid` pseudo-class selects controls that are candidates for constraint validation and satisfy their constraints. It also selects forms with no invalid associated controls and `fieldset` elements with no invalid descendant controls. It does not require prior user interaction.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

:valid

:valid is a pseudo-class without arguments: write it without parentheses, for example input:valid. In HTML, it matches elements that are candidates for constraint validation and satisfy their constraints, as well as forms and fieldset elements under the conditions below. It can style valid fields, but does not itself establish validation rules.

The result depends on factors including required, type, pattern, and a custom error set through setCustomValidity(). An empty optional type="email" field can be valid, so :valid does not mean “filled in.” Nor is it equivalent to :not(:invalid): an element outside constraint validation can match neither validity pseudo-class.

form:valid matches a form if none of its associated controls that are candidates for constraint validation fail their constraints. This includes forms with no associated controls that are candidates for constraint validation.

fieldset:valid matches a fieldset if none of its descendants that are candidates for constraint validation fail their constraints. The fieldset itself does not have to be a candidate for constraint validation to match.

:valid can apply immediately on page load; :user-valid additionally considers a state related to user interaction.

In the example, A12 satisfies pattern, but B12 does not, so the whole form does not match :valid. The border updates with the field's state, but the diagnostic text is calculated only once. Do not communicate validity through color alone, and do not treat browser validation as a replacement for server-side validation.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:valid example</title>
<style>
  input { border: 3px solid gray; }
  input:valid { border-color: green; }
</style>
<form id="codes">
  <label>Code A <input id="a" required pattern="A[0-9]{2}" value="A12"></label>
  <label>Code B <input id="b" required pattern="A[0-9]{2}" value="B12"></label>
</form>
<pre id="result"></pre>
<script>
  const a = document.getElementById('a');
  const b = document.getElementById('b');
  const codes = document.getElementById('codes');
  document.getElementById('result').textContent = [
    `a: ${a.matches(':valid')}`,
    `b: ${b.matches(':valid')}`,
    `codes: ${codes.matches(':valid')}`
  ].join('\n');
</script>
</html>

Result

a: true
b: false
codes: false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources