Min and Max are not working as expected on input='time'

huangapple go评论67阅读模式
英文:

Min and Max are not working as expected on input='time'

问题

关于minmax属性在<input type="time"/>中的工作原理,我有一些疑惑。使用这段代码

<input type="time" id="appt" name="appt" min="09:00" max="18:00" required>

我期望在9:00之前和18:00之后的时间不可选。但事实并非如此。你可以在mdn web docs上尝试。我在Chrome、FireFox和Edge上进行了测试。

我在这里遗漏了什么?

英文:

I am a little bit confused about how the min and max attributes work, when &lt;input type=&quot;time&quot;/&gt; is set. With this code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

<!-- language: lang-html -->

&lt;input type=&quot;time&quot; id=&quot;appt&quot; name=&quot;appt&quot; min=&quot;09:00&quot; max=&quot;18:00&quot; required&gt;

<!-- end snippet -->

I would expect a time before 9:00 and and after 18:00 to not be selectable. But it is. You can try that on the mdn web docs. I tested it on Chrome, FireFox and Edge.

What am I missing here?

答案1

得分: 1

内置表单验证发生在表单提交时(即点击提交按钮时)或调用 reportValidity 时。

它不会阻止首次输入无效数据,只是在数据发送到服务器之前捕获它。

此截图显示了在Firefox中的输出:

Min and Max are not working as expected on input='time'

<form>
  <input type="time" id="appt" name="appt" min="09:00" max="18:00" required value="08:00">
  <button>提交</button>
</form>
英文:

Built-in form validation takes place when the form is submitted (i.e. when the submit button is clicked) or when reportValidity is called.

It doesn't prevent invalid data being entered in the first place. It just catches it before it is sent to the server.

This screenshot shows the output in Firefox:

Min and Max are not working as expected on input='time'

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setTimeout(() =&gt; document.querySelector(&#39;form&#39;).reportValidity(), 50);

<!-- language: lang-html -->

&lt;form&gt;
  &lt;input type=&quot;time&quot; id=&quot;appt&quot; name=&quot;appt&quot; min=&quot;09:00&quot; max=&quot;18:00&quot; required value=&quot;08:00&quot;&gt;
  &lt;button&gt;Submit&lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 17:49:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定