Is there a minlength validation attribute in HTML5?

It seems the minlength attribute for an <input> field doesn't work.

Is there any other attribute in HTML5 with the help of which I can set the minimal length of a value for fields?


You can use the pattern attribute. The required attribute is also needed, otherwise an input field with an empty value will be excluded from constraint validation.

<input pattern=".{3,}"   required title="3 characters minimum">
<input pattern=".{5,10}" required title="5 to 10 characters">

If you want to create the option to use the pattern for "empty, or minimum length", you could do the following:

<input pattern=".{0}|.{5,10}" required title="Either 0 OR (5 to 10 chars)">
<input pattern=".{0}|.{8,}"   required title="Either 0 OR (8 chars minimum)">

There is a minlength property in HTML5 spec now, as well as the validity.tooShort interface.

PS This hasn't been implemented as of Firefox 33.0a1 and Chrome 38.0.2071.0 canary.

Update : HTML5 has been stable and Chrome has minlength and validity.tooShort enabled by default since version 40.


Here is HTML5-only solution (if you want minlength 5, maxlength 10 character validation)

http://jsfiddle.net/xhqsB/102/

<form>
  <input pattern=".{5,10}">
  <input type="submit" value="Check"></input>
</form>
链接地址: http://www.djcxy.com/p/29266.html

上一篇: 如何在MATLAB中获取变量的类型?

下一篇: HTML5中是否有最小长度验证属性?