I am trying to give an input element different border types depending on whether the input element is either empty, invalid or valid. My code is as follows:
The styling works only if I omit the empty pseudoclass, in which case the system treats an empty input element as invalid.
Does anyone have any idea what I am doing wrong?
A number type input cant be empty? (No, that’s not serious.)
The syntax appears correct, other than the missing space between the selector and the open bracket…
What does the HTML element look like? Cause… valid/invalid implies you’ve got a test running on the field…
<label for="HourlyRate">Hourly Rate *</label>
<input type="number" id="HourlyRate" name="HourlyRate" placeholder="Rate: between 20 and 50" min="20" max="50" required>
<label for ="HoursWorked">Hours Worked *</label>
<input type="number" id="HoursWorked" name="HoursWorked" placeholder="Hours Worked: between 5 and 60" min="5" max="60" required>
I want the data in the HourlyRate element to be between 20 and 50 inclusive. Any values outside of this range should render the element invalid - which it does.
Same applies to the HoursWorked.
Any idea how to convey to the user that an input element has had no data entered into it?
Or is it the case that a number type input is always invalid until valid data is entered.
This is almost like the Catholic theology about original sin.
Rather than checking empty, check and see if your field’s placeholder is showing. If so, make the border black. If not, evaluate valid/invalid. (Note that the order of these is important - the field is both invalid and showing the placeholder while the field is empty, so you want the black to override the red.)
Inputs are always :empty because their content is provided via their attributes. They do not have content as such and therefore are always :empty. They are not much use for form validation in this way.
The placeholder-shown method posted by @m_hutley above should do what you want.