Html 如何在我的 html5 日期输入上设置最大值,以便 iOS Safari 可以识别它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8417199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:51:55  来源:igfitidea点击:

how can I set a max value on my html5 date input so that it could be recognized by safari for iOS?

ioshtmldateinputdatepicker

提问by Alexis

The title is pretty clear I guess?

标题说的很清楚了吧?

Here's my date input :

这是我的日期输入:

<input type="date" id="birthdate" name="birthdate" value="" max="1993-12-07" required/>

In chrome, I cannot enter a date superior to 1993-12-07 using the little arrows of the field (but if I write the date myself I can).

在 chrome 中,我无法使用字段的小箭头输入高于 1993-12-07 的日期(但如果我自己写日期,我可以)。

In safari for iOS, focusing that field just brings a nice datepicker. However, this datepicker is NOT limited to the "max" attribute. So how can I do that? Of course, I have a server-side form validation but I want to make it on client-side too. Any idea? Thanks!

在 iOS 的 safari 中,关注该领域只会带来一个不错的日期选择器。但是,此日期选择器不限于“max”属性。那我该怎么做呢?当然,我有一个服务器端表单验证,但我也想在客户端进行验证。任何的想法?谢谢!

回答by aWebDeveloper

Safari does not yet support it yet . Only Opera and chrome support it as of Nov 2011. Anyways here is the w3spec on it http://www.w3.org/TR/html-markup/input.date.html#input.date.attrs.max

Safari 尚不支持它。截至 2011 年 11 月,只有 Opera 和 chrome 支持它。无论如何这里是关于它的 w3spec http://www.w3.org/TR/html-markup/input.date.html#input.date.attrs.max

You can use js validation on submit or on change. Use a js validation framework of your choice

您可以在提交或更改时使用 js 验证。使用您选择的 js 验证框架

Here is pure js example http://www.roseindia.net/answers/viewqa/Ajax/4734-start-date-and-end-date-validation-in-javascript.html. Use your max date instead of end date

这是纯 js 示例http://www.roseindia.net/answers/viewqa/Ajax/4734-start-date-and-end-date-validation-in-javascript.html。使用您的最大日期而不是结束日期

For jquery solution look at this Validate that end date is greater than start date with jQuery

对于 jquery 解决方案,请查看此使用 jQuery 验证结束日期大于开始日期

回答by Luqman Cheema

Found the Solution for dynamically disabling future dates:

找到了动态禁用未来日期的解决方案:

@{ string datestring = DateTime.Now.ToString("yyyy-MM-dd"); }
<input type="date" 
       required="required" 
       name="AttendanceDate" 
       max="@datestring" 
       class="form-control" />