7

Consider the following html:

<input bar value="bar">

<input foo readonly value="foo">

The weird thing here is that the first input element is valid and the second one is not just because it is readonly!

$('[bar]').is(':valid') === true

$('[foo]').is(':valid') === false

DEMO/JSFIDDLE

What is going on here ? And how can I fix this ?

Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

10

Readonly inputs are barred from constraint validation, according to HTML5 docs.

This means, a readonly input is neither valid nor invalid.

Here some code which demonstrates it (see fiddle):

HTML:

<input type="email" value="invalidemail">
<input type="email" value="valid@e.mail">

<input type="email" readonly value="invalidemail">
<input type="email" readonly value="valid@e.mail">

CSS:

input:invalid {
    background-color: red;
}
input:valid {
    background-color: green;
}
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • 1
    thanks a lot, it makes more sense now :) I've updated the jsfiddle a bit [here](http://jsfiddle.net/n1vykgxg/12/) to visualize it even more! – Jeanluca Scaljeri Oct 24 '14 at 20:50