Search code examples
javascripthtmlcheckvalidity

HTML5 checkValidity says an empty input is valid


Given the following document:

<input pattern="[a-z]"/>

Without filling in the input, running:

document.querySelector('input').checkValidity()

Returns true.

Since an empty input would not match [a-z], why is HTML5 checkValidity() returning true?


Solution

  • Use required in order to not validate empty input.

    <input pattern="[a-z]" required />