Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an input field valid while is empty?

Tags:

html

css

I've been experimenting with css pseudo classes and pseudo elements and I discover something that I have not been able to get around.

Consider the following html input field:

<input type="number" value="1" min="1" max="1000" step="1" pattern="\d" /><span></span>

In order to achieve 3 states: empty, valid and invalid. I'm using the ::after pseudo element (applied to the span adjacent to the input) to add a checkmark when the value of the filed is valid, and an X when the value is invalid.

I'm using the pseudo classes :valid and :invalid and it seems that when an input field is empty (value="") its state is also valid.

The CSS in question is as follows:

.v3_forms input[value=""] + span::after {
    content: "";
}
.v3_forms input:valid + span::after {
    content: "\2713";
    color: limegreen;
}
.v3_forms input:invalid + span::after {
    content: "X";
    color: #ce0000;
}

For what I'm able to tell, after clearing the value in the browser, the second css rules takes precedence, even though the specificity is the same.
I tested the selectors here: specificity calculator and it seems that attributes and pseudo classes have the same weight.

like image 823
jnkrois Avatar asked Jul 11 '15 05:07

jnkrois


People also ask

What is empty input?

We want to know whether an input is empty. For our purpose, empty means: The user hasn't typed anything into the field. The user has typed one or more empty spaces, but not other characters.

How do I make a field invalid?

setCustomValidity("Invalid field."); will make the field invalid. field. setCustomValidity(""); will make the field valid unless it fails an HTML5 constraint. Also, if you want the browser's validity message to show up on input or blur you can use field.

How do I check if a python input is empty?

Use an if statement to check if a user input is empty, e.g. if country == '': . The input() function is guaranteed to return a string, so if it returns an empty string, the user didn't enter a value.


1 Answers

An empty string is, by default, a valid value:

User agents should allow the user to set the value to the empty string *.

If you want to require a value for the field, you need to add the required attribute:

.v3_forms input[value=""] + span::after {
    content: "";
}
.v3_forms input:valid + span::after {
    content: "\2713";
    color: limegreen;
}
.v3_forms input:invalid + span::after {
    content: "X";
    color: #ce0000;
}
<div class="v3_forms">
<input type="number" value="1" min="1" max="1000" step="1" pattern="\d" required /><span></span>
</div>

This addresses the specific example in your question, but the same will hold true for any of the <input /> elements that accept user input.

*See the whatwg reference.

like image 143
Tieson T. Avatar answered Sep 23 '22 00:09

Tieson T.