Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't input minlength check work with initial value?

Tags:

html

Consider the following form:

<form>
  <input type="text" minlength="5" value="1234">
  <button type="submit">submit</button>
</form>

When I click the submit button without changing anything, the minimum length validation doesn't work and the form submits successfully.

But after changing the input value, e.g. 1234 -> 12345 -> 1234, the validation works and the form does not get submitted.

Why?

like image 218
mitsuruog Avatar asked Aug 29 '17 02:08

mitsuruog


People also ask

Why Minlength is not working in HTML?

The minLength attribute (unlike maxLength) does not exist natively in HTML5. However, there a some ways to validate a field if it contains less than x characters. Show activity on this post.

How do you set the Minlength of an input type number in HTML?

The minlength attribute specifies the minimum number of characters required in an input field. Note: The minlength attribute can be used with input type: text, search, url, tel, email, and password.

How do I validate the length of a field in HTML?

Use the minlength Attribute to Set the Minimum Length Validation in HTML. HTML provides an attribute called minlength to set the minimum length validation in the input field. It works in all the input types, just as the pattern attribute. For example, create a form and then an input field for a password.

What is Minlength HTML?

The minlength attribute defines the minimum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no minlength is specified, or an invalid value is specified, the input has no minimum length.


2 Answers

This is by design. The minlength attribute only validates a field once it has been edited by the user. It doesn't validate the field if its value hasn't been changed, even if that value doesn't meet the constraint. From the spec (emphasis mine):

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.

If you need to validate the value regardless of whether the user has since edited the field, see Racil Hilan's answer (although their statement about the minlength attribute not being supported everywhere doesn't imply anything and is largely irrelevant — as shown, this is clearly by design; if anything it shows that the browsers that do support the attribute support it fully).

like image 116
BoltClock Avatar answered Sep 23 '22 12:09

BoltClock


The minlength attribute is not supported in all browsers. You can use the pattern attribute instead. The required attribute is also needed, otherwise an input field with an empty value will be excluded from the validation.

Try this:

<form>
  <input type="text" pattern=".{5,}" required value="1234">
  <button type="submit">submit</button>
</form>

The added benefit of using the pattern attribute is that it validates initial values, so you will not have the issue that you've seen with the minlength attribute which doesn't validate initial values (as explained in details by BoltClock's answer). The downside, though, is that the validation message is not as elegant. For example, the message in Chrome is "Please match the requested format" for pattern and "Please lengthen this text to 5 characters or more" for minlength.

like image 43
Racil Hilan Avatar answered Sep 24 '22 12:09

Racil Hilan