html - 如何在输入 [type ="number"] 上使用模式?

标签 html css validation input

我希望默认值为 0,如果输入大于 0,则使用 :valid 重新着色输入。难道我做错了什么?谷歌浏览器错了吗?还是 HTML5 规范?我想在没有 JS 的情况下解决这个问题。

:valid {
  background: green
}
<p>This should not be green, but it is:</p>
<input max="100" min="0" pattern="^[1-9]\d*$" type="number" value="0" />

<p>Works perfectly, if the type is changed to text:</p>
<input pattern="^[1-9]\d*$" type="text" value="0" />

最佳答案

Citing MDN on the pattern property (添加了突出显示):

pattern

A regular expression that the control's value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is text, search, tel, url or email; otherwise it is ignored. The regular expression language is the same as JavaScript's. The pattern is not surrounded by forward slashes.

有趣的是,我在 W3C 中都找不到任何提及和 WhatWG标准。

关于html - 如何在输入 [type ="number"] 上使用模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411753/

相关文章:

javascript - 图片上方的半透明div

html - 单击链接时纯 HTML/CSS 下拉菜单关闭(链接不打开)

linux - 如何验证脚本中的视频文件?

html - 鼠标单击缩略图时,右侧的缩略图向左跳转

php - PHP 中用于输入验证的正则表达式

javascript - 使用选择和文本输入进行 JQuery 验证

html - 当前一个 div 文本溢出时,内联 div 行的第二个 div 从下一行开始而不是在前一个 div 之后

javascript - jQuery if 语句触发两次

javascript - 根据 div 的值填充 div

html - 如何使用模式在表中显示数据库中的数据