<input>
字段的minlength
属性似乎不起作用。
HTML5中有没有其他属性可以帮助我设置字段的值的最小长度?
发布于 2012-04-24 16:24:59
您可以使用pattern
attribute。还需要required
attribute,否则将从constraint validation中排除具有空值的输入字段。
<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)">
发布于 2018-10-28 00:47:15
我使用了带或不带required
的maxlength和minlength,它对我来说在HTML5上工作得很好。
<input id="passcode" type="password" minlength="8" maxlength="10">
`
发布于 2012-04-23 22:11:47
minLength属性(与maxLength不同)在HTML5中本身不存在。但是,如果字段包含的字符少于x,则有一些方法可以验证该字段。
在此链接中给出了一个使用jQuery的示例:http://docs.jquery.com/Plugins/Validation/Methods/minlength
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});;
</script>
<script>
$(document).ready(function(){
$("#myform").validate({
rules: {
field: {
required: true,
minlength: 3
}
}
});
});
</script>
</head>
<body>
<form id="myform">
<label for="field">Required, Minimum length 3: </label>
<input class="left" id="field" name="field" />
<br/>
<input type="submit" value="Validate!" />
</form>
</body>
</html>
https://stackoverflow.com/questions/10281962
复制相似问题