HTML5中是否有最小长度验证属性? [英] Is there a minlength validation attribute in HTML5?

查看:538
本文介绍了HTML5中是否有最小长度验证属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎< input> 字段的 minlength 属性不起作用。

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

HTML5中是否有其他属性可以帮助我设置字段值的最小长度?

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

推荐答案

您可以使用 pattern 属性 要求<还需要/ code>属性,否则将从约束验证

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)">

这篇关于HTML5中是否有最小长度验证属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆