Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is readonly input field not valid

Tags:

html

css

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 ?

like image 339
Jeanluca Scaljeri Avatar asked Oct 24 '14 20:10

Jeanluca Scaljeri


People also ask

How do I make input fields read-only?

The readonly attribute of <input> element in HTML is used to specify that the input field is read-only. If an input is readonly, then it's content cannot be changed but can be copied and highlighted. It is a boolean attribute. Example: This example uses HTML <input> readonly Attribute.

Does readonly input get submitted?

Save this answer. Show activity on this post. Disabled means that no data from that form element will be submitted when the form is submitted. Read-only means any data from within the element will be submitted, but it cannot be changed by the user.

How do I make input readonly in react?

You can make the TextBox as read-only by setting the readonly attribute to the input element.

What is the difference between a readonly input and a disabled input?

The difference between disabled and readonly is that read-only controls can still function and are still focusable, whereas disabled controls can not receive focus and are not submitted with the form and generally do not function as controls until they are enabled.


1 Answers

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="[email protected]">

<input type="email" readonly value="invalidemail">
<input type="email" readonly value="[email protected]">

CSS:

input:invalid {
    background-color: red;
}
input:valid {
    background-color: green;
}
like image 87
Alex Shesterov Avatar answered Sep 24 '22 23:09

Alex Shesterov