HTML5 constraint-validation fails when data is pre-filled

后端 未结 1 1898
别那么骄傲
别那么骄傲 2021-01-25 03:59

I\'m currently struggeling with HTML5 constraint validation.

Given this simple form:

相关标签:
1条回答
  • 2021-01-25 04:24

    The maxlength seems only to work after some changes are done on the content of the <input> element. You can also use pattern to define the maxlength of the <input>:

    <form action="/" method="post">
        <input type="text" name="comeGetSome" value="This is way too long" required="required" pattern=".{1,10}" /><br />      
        <input type="submit" value="Send me" />
    </form>

    The constraint is evaluated only when the value of the attribute has been changed.
    source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-maxlength

    Constraint validation: If an element has a maximum 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), and the JavaScript string length of the element's API value is greater than the element's maximum allowed value length, then the element is suffering from being too long.
    source: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength

    0 讨论(0)
提交回复
热议问题