正则表达式 |闰年及更多 [英] Regular Expression | Leap Years and More

查看:22
本文介绍了正则表达式 |闰年及更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在寻找一种正则表达式来进行一些客户端日期检查,但一直找不到满足以下条件的正则表达式:

I've recently been looking for a regular expression to do some client side date checking, and I haven't been able to find one that can satisfy the following criteria:

  • 范围从 1800 到现在
  • 使用闰年执行正确的日期检查
  • MM/DD/YYYY 表单
  • 无效的日期检查

(这些限制超出了我的范围,是客户的要求,尽管我努力说服他们这不是最佳途径)

当前代码:

$('input').keyup(function()
{
       var regex = /^(?:(0[1-9]|1[012])[/.](0[1-9]|[12][0-9]|3[01])[/.](18|19|20)[0-9]{2})$/;
       $(this).toggleClass('invalid',!regex.test($(this).val()));    
});

更新:

我应该注意,这主要是为了看看这样的正则表达式是否可行(因为在这件事上我不选择使用正则表达式).我知道用于验证日期的其他(和更好)选项,但是如前所述 - 这是为了查看是否可以通过正则表达式.

I should note that this is primarily to see if a regular expression like this would be possible (as the use of a Regex is not my choice in this matter). I am aware of the other (and better) options for validating a date, however as previously mentioned - this is to see if it was possible through a regular expression.

推荐答案

正如在别处提到的,正则表达式几乎肯定不是你想要的.但是,话虽如此,如果你真的想要一个正则表达式,它是如何构建的:

As is mentioned elsewhere, regular expressions almost certanily not what you want. But, having said that, if you really want a regular expression, here is how it is built:

每月 31 天

(0[13578]|1[02])[/.](0[1-9]|[12][0-9]|3[01])[/.](18|19|20)[0-9]{2}

30 天月

(0[469]|11)[/.](0[1-9]|[12][0-9]|30)[/.](18|19|20)[0-9]{2}

2 月 1-28 日始终有效

February 1-28 always valid

(02)[/.](0[1-9]|1[0-9]|2[0-8])[/.](18|19|20)[0-9]{2}

2 月 29 日也适用于闰年

February 29 also valid on leap years

(02)[/.]29[/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000)

这意味着如果你把它们放在一起就是这个:

which means it would be this if you put it all together:

((0[13578]|1[02])[/.](0[1-9]|[12][0-9]|3[01])[/.](18|19|20)[0-9]{2})|((0[469]|11)[/.](0[1-9]|[12][0-9]|30)[/.](18|19|20)[0-9]{2})|((02)[/.](0[1-9]|1[0-9]|2[0-8])[/.](18|19|20)[0-9]{2})|((02)[/.]29[/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))

这个版本有点短,但有点难理解.

This version is a little shorter, but a little harder to understand.

((0[13578]|1[02])[/.]31[/.](18|19|20)[0-9]{2})|((01|0[3-9]|1[1-2])[/.](29|30)[/.](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[/.](0[1-9]|1[0-9]|2[0-8])[/.](18|19|20)[0-9]{2})|((02)[/.]29[/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))

这些脚本很长而且无法维护.应该清楚,这不是一个好主意,但它是可能的.

These scripts are long and unmaintainable. It should be clear that this isn't a good idea, but it is possible.

注意事项:

  • 范围 1800-2099(可以添加更多,没有太多困难,但需要在 4-6 个不同的地方进行更改)
  • 需要 2 位数的月和日(可以从表达式中删除 ~8 处的严格性)
  • [/.] 作为分隔符(8 个位置)
  • 尚未经过测试(我们可以针对所有数字组合进行检查并与 javascript 日期函数进行比较?[证明我们正在重新发明轮子])
  • range 1800-2099 (more can be added without too much difficulty, but requires changes in 4-6 disparate places)
  • requires 2 digit months and days (the strictness could be removed from the expression in ~8 places)
  • [/.] as seperators (8 places)
  • Hasn't been tested (we could check it against all digit combinations and compare with the javascript date function? [proof that we're reinventing the wheel])

这篇关于正则表达式 |闰年及更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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