Closed Bug 1526404 Opened 7 years ago Closed 7 years ago

minlength validation is not triggered when value is changed with JavaScript

Categories

(Core :: DOM: Core & HTML, defect)

65 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

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.

Hi John, could you help check where our code goes wrong (if we do)? Thanks!

Flags: needinfo?(jdai)

FWIW, I got the same behavior on Chrome and Safari.

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

Flags: needinfo?(jdai)

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.

Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → INVALID
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.