如何验证HTML5日期格式 [英] How to validate HTML5 date format

查看:671
本文介绍了如何验证HTML5日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用HTML5日期输入字段

I want to use the HTML5 date input field

<input type='date' name='customDate'>

以便其他用户可以使用浏览器中的内置日期选择器。

So that other users can make use of the build-in datepicker from their browser.

我想检查输入是否实际上是日期格式。我在这里找到:有没有办法改变输入类型= QUOT;日期"格式?没有独特的演示格式。例如,在Chrome中,日期字段的输入以 dd.mm.yyyy 的形式提供,在Firefox 24或IE 9/10中,输入字段中的日期显示为 YYYY-MM-DD

I would like to check if the input is actually in date format. As I found here: Is there any way to change input type="date" format? there is no unique presentation format. For example, in Chrome the input of the date field is given in the form of dd.mm.yyyy and in Firefox 24 or IE 9/10 the date in the input field is presented as YYYY-MM-DD.

我的第一个问题是,您如何告诉用户您希望他在日期中输入哪种格式?在Firefox中,我需要类似

My first problem is, how do you tell the user in which format you want him to type in the date? In Firefox I would need something like

<label>Enter Date(YYYY-MM-DD)</label><input type='date' 
                                         name='customDate' placeholder='YYYY-MM-DD'>

但这对Chrome来说是错误的。

But this would be wrong for Chrome.

其次,如果当前输入是(浏览器的)有效日期格式,如何在提交前检查?
我知道如果日期格式为YYYY-MM-DD,我可以在提交后查看PHP,但在提交之前如何用JavaScript检查?

Secondly, how can I check before submission if the current input is in the valid date format (of the browser)? I know that I could check with PHP after submit if the date format is given as YYYY-MM-DD, but how do you check it with JavaScript before submission?

推荐答案

像这样使用:

<label>Enter Date(YYYY-MM-DD)</label>
<input type='date' name='customDate' placeholder='YYYY-MM-DD' pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))" >

对于所有其他HTML5日期模式请访问:

For all other HTML5 Dates Pattern Please visit this:

http://html5pattern.com/Dates

这篇关于如何验证HTML5日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆