且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用JavaScript检查日期格式

更新时间:2022-04-10 07:28:35

您可以使用正则表达式:

You can use a regex:

/^\d{2}\/\d{2}\/\d{4}$/.test(str)

当然,这不会检查有效日期,但是在这种情况下,您始终可以让服务器端代码抛出错误.如果您想进行更多的客户端验证,请查看 Javascript:如何验证格式为MM-DD-YYYY的日期?;很容易适应您的格式.

Of course this will not check for a valid date, but in that case you can always let your server-side code throw an error. If you want more client-side validation have a look at Javascript: how to validate dates in format MM-DD-YYYY?; it is easy to adapt to your format.