emi*_*lan 587 html5 html5-validation
看起来字段的minlength属性<input>不起作用.
HTML5中是否还有其他属性可以帮助我设置字段值的最小长度?
use*_*621 1283
您可以使用该pattern属性.该required属性也是必需的,否则将从约束验证中排除具有空值的输入字段.
<input pattern=".{3,}" required title="3 characters minimum">
<input pattern=".{5,10}" required title="5 to 10 characters">
Run Code Online (Sandbox Code Playgroud)
如果要创建将模式用于"空或最小长度"的选项,则可以执行以下操作:
<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)">
Run Code Online (Sandbox Code Playgroud)
rhg*_*hgb 150
目前是一个minlength房地产HTML5规范现在,还有validity.tooShort接口.
两者现在都在所有现代浏览器的最新版本中启用.有关详细信息,请参阅https://caniuse.com/#search=minlength.
Ali*_*ğlu 17
这是HTML5专用解决方案(如果你想要minlength 5,maxlength 10字符验证)
http://jsfiddle.net/xhqsB/102/
<form>
<input pattern=".{5,10}">
<input type="submit" value="Check"></input>
</form>
Run Code Online (Sandbox Code Playgroud)
Soh*_*edM 11
是的,就是这样.这就像maxlength.W3.org文档:http: //www.w3.org/TR/html5/forms.html#attr-fe-minlength
如果minlength不起作用,请使用pattern@ Pumbaa80提到的属性作为input标记.
对于textarea:
设置最大值; 使用maxlength和分钟去这个链接.
你会在这里找到最大和最小.
我使用或不使用 maxlength 和 minlength,required它对我来说非常适合 HTML5。
<input id="passcode" type="password" minlength="8" maxlength="10">Run Code Online (Sandbox Code Playgroud)
`
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>
Run Code Online (Sandbox Code Playgroud)
不是HTML5,但实际上是实用的:如果你碰巧使用AngularJS,你可以使用ng-minlength输入和textareas.另见Plunk.
我注意到有时在 Chrome 中,当自动填充打开并且字段是自动填充浏览器内置方法的字段时,它会绕过最小长度验证规则,因此在这种情况下,您必须通过以下属性禁用自动填充:
自动完成=“关闭”
<input autocomplete="new-password" name="password" id="password" type="password" placeholder="Password" maxlength="12" minlength="6" required />
Run Code Online (Sandbox Code Playgroud)
minlength属性现在在大多数浏览器中得到广泛支持。
<input type="text" minlength="2" required>
Run Code Online (Sandbox Code Playgroud)
但是,与其他 HTML5 功能一样,此全景图中缺少 IE11。因此,如果您拥有广泛的 IE11 用户群,请考虑使用大多数浏览器(包括 IE11)pattern几乎全面支持的HTML5 属性。
为了有一个漂亮而统一的实现,并且可能是可扩展的或动态的(基于生成 HTML 的框架),我会投票支持该pattern属性:
<input type="text" pattern=".{2,}" required>
Run Code Online (Sandbox Code Playgroud)
还有一个小的可用性捕捉使用时pattern。用户在使用pattern. 请参阅此 jsfiddle或以下内容:
<input type="text" minlength="2" required>
Run Code Online (Sandbox Code Playgroud)
例如,在 Chrome 中(但在大多数浏览器中类似),您将收到以下错误消息:
Please lengthen this text to 2 characters or more (you are currently using 1 character)
Run Code Online (Sandbox Code Playgroud)
通过使用minlength和
Please match the format requested
Run Code Online (Sandbox Code Playgroud)
通过使用pattern.
| 归档时间: |
|
| 查看次数: |
572429 次 |
| 最近记录: |