minlength validation is not triggered when value is changed with JavaScript
Categories
(Core :: DOM: Core & HTML, defect)
Tracking
()
People
(Reporter: jason.barnabe, Unassigned)
Details
When using JavaScript to set a value on an input with minlength, the input is not marked as invalid unless the user interacts with the field.
<style>input:invalid { background-color: red;}</style>
<input id=test minlength=5>
<script>document.getElementById("test").value = '1234'</script>
Contrast this with a pattern validation, where the input is immediately marked as invalid.
<style>input:invalid { background-color: red;}</style>
<input id=test pattern=".{5,}">
<script>document.getElementById("test").value = '1234'</script>
I thought maybe it was because minlength is controller by the dirty value flag and pattern isn't, but it sounds like the dirty value flag should affected by script changes.
Comment 1•7 years ago
|
||
Hi John, could you help check where our code goes wrong (if we do)? Thanks!
Comment 2•7 years ago
|
||
FWIW, I got the same behavior on Chrome and Safari.
Comment 3•7 years ago
|
||
IIRC, our code is following the spec[1], it did the corresponding check for the value which changed by a user edit[2].
Constraint validation: If an element has a minimum allowed value length, its dirty value flag is true, its value was last changed
by a user edit (as opposed to a change made by a script), its value is not the empty string, and the JavaScript string length of
the element's API value is less than the element's minimum allowed value length, then the element is suffering from being too
short.
[1] https://html.spec.whatwg.org/#setting-minimum-input-length-requirements:-the-minlength-attribute
[2] https://searchfox.org/mozilla-central/rev/00c0d068ece99717bea7475f7dc07e61f7f35984/dom/html/HTMLInputElement.cpp#6527
Comment 4•7 years ago
|
||
Thanks John! According to the comment, close as invalid. Dear reporter, if you want to request a change, please file a issue on the standard.
| Assignee | ||
Updated•6 years ago
|
Description
•