<input>字段的minlength属性似乎不起作用。
在HTML中是否有其他属性可以帮助我设置字段值的最小长度?
<input>字段的minlength属性似乎不起作用。
在HTML中是否有其他属性可以帮助我设置字段值的最小长度?
当前回答
现在HTML5规范中有一个minlength属性,以及有效性。tooShort接口。
现在所有现代浏览器的最新版本都启用了这两个功能。具体操作请参见https://caniuse.com/#search=minlength。
其他回答
如果需要这种行为,总是在输入字段上显示一个小前缀,否则用户不能删除前缀:
// prefix="prefix_text"
// If the user changes the prefix, restore the input with the prefix:
if(document.getElementById('myInput').value.substring(0,prefix.length).localeCompare(prefix))
document.getElementById('myInput').value = prefix;
您可以使用pattern属性。还需要必需的属性,否则带有空值的输入字段将被排除在约束验证之外。
<input pattern=".{3,}" required title="3 characters minimum">
<input pattern=".{5,10}" required title="5 to 10 characters">
如果你想创建一个选项来使用“空,或最小长度”的模式,你可以这样做:
<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)">
看到http://caniuse.com/搜索=最小长度。某些浏览器可能不支持此属性。
如果"type"的值是其中之一:
文本,电子邮件,搜索,密码,电话或URL(警告:不包括数字|没有浏览器支持“电话”现在- 2017.10)
使用minlength(/ maxlength)属性。它指定最小字符数。
例如,
<input type="text" minlength="11" maxlength="11" pattern="[0-9]*" placeholder="input your phone number">
或者使用"pattern"属性:
<input type="text" pattern="[0-9]{11}" placeholder="input your phone number">
如果“type”是number,虽然不支持minlength(/ maxlength),但可以使用min(/ max)属性来代替它。
例如,
<input type="number" min="100" max="999" placeholder="input a three-digit number">
现在HTML5规范中有一个minlength属性,以及有效性。tooShort接口。
现在所有现代浏览器的最新版本都启用了这两个功能。具体操作请参见https://caniuse.com/#search=minlength。
我使用maxlength和minlength,无论是否需要,它对我来说都非常适合HTML5。
<输入id=“passcode”类型=“minlength”密码=“8”,maxlength=“10”>
`