Is there invalid state of Input range?
Is there invalid state of Input range?
I need to define custom HTML5 form validation styles in my application's design system like Bootstrap. But I don’t know how to apply :invalid state to an <input type="range">.
:invalid
<input type="range">
console.log($('#test').val());
$('#test').val(50);
console.log($('#test').val());
.valid-message,
.invalid-message
display: none;
.input:valid ~ .valid-message
display: block;
color: green;
.input:invalid ~ .invalid-message
display: block;
color: red;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control">
<input type="range" class="input range" id="test" min="1" max="20">
<p class="valid-message">This is valid</p>
<p class="invalid-message">This is invalid</p>
</div>
Is there invalid state of Input range? How can I reproduce that state?
I did a bit of testing, but I can't seem to create an invalid range input. Setting the value to an out of range one (including by adding a
value="50" attribute to the source) results in the value being snapped to the min or max value. ( In other words, such an input is always valid. However, I'd love to be shown wrong!– Mr Lister
Aug 27 at 9:51
value="50"
I think @MrLister is right, you can't set invalid value on client side. From mdn "If an attempt is made to set the value lower than the minimum, it is set to the minimum. Similarly, an attempt to set the value higher than the maximum results in it being set to the maximum."
– Esko
Aug 27 at 9:55
@SuperDJ Thanks, but as @mr-lister said, I can not reproduce the
:out-of-range.– Alex
Aug 27 at 9:56
:out-of-range
For testing purposes, if you want to simulate what would happen if those states could be triggered (during debugging), just give the input
type="number". Then it works similarly, except you can give it an invalid value.– Mr Lister
Aug 27 at 10:09
type="number"
3 Answers
3
Range controls can be invalid only if setCustomValidity() is called.
setCustomValidity()
Refer to the specification; https://html.spec.whatwg.org/multipage/input.html#concept-input-value-default-range
It never be valueMissing state because it has default value.
It never be rangeUnderflow, rangeOverflow, stepMismatch because UA must adjust the value according to the specification.
valueMissing
rangeUnderflow
rangeOverflow
stepMismatch
I could be confirmed the invalid state by
document.getElementById("test").setCustomValidity("Error message"). I learned new here, I can ignore designing the invalid style of range input. Thanks!– Alex
Aug 28 at 10:42
document.getElementById("test").setCustomValidity("Error message")
But I did not know how to apply :invalid state to an <input type="range">.
:invalid
<input type="range">
Actually you can't apply :invalid state to an <input type="range">, because the value will always be set between the min and max values, and you can't force it to be out of range it will be automatically updated to fit the range.
:invalid
<input type="range">
min
max
range
If you check the MDN <input type="range"> specifications in the Value section, you can see that:
<input type="range">
If an attempt is made to set the value lower than the minimum, it is set to the minimum. Similarly, an attempt to set the value higher than the maximum results in it being set to the maximum.
By testing on Chrome and Firefox I reason that no :invalid state can occur for type="range". Any value out of range is automatically increased or decreased to min or max.
:invalid
type="range"
min
max
Mozilla sets the defaults to 0 and 100 and bad or no values are replaced by average value.
W3C about input range
Can I see the specifications ? W3C or etc.
– Alex
Aug 27 at 11:07
@Alex I've rewritten my answer to be more accurate.
– Niklaus Hug
Aug 27 at 12:52
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Maybe you can use developer.mozilla.org/en-US/docs/Web/CSS/:out-of-range
– SuperDJ
Aug 27 at 9:42