用于测试日期格式的正则表达式
我在正则表达式示例网站中发现了一个非常有用的正则表达式,用于测试日期字段的格式和内容
,但是当我输入 2000 年之前的日期时,我会得到验证,因为这是一个用于输入出生日期的字段,您可以明白为什么它会是一个问题。我确信这是一个简单的解决方案,但正则表达式让我感到害怕。
$('#txtDOB').blur(function() {
//$('span.error-keyup-5').remove();
var inputVal = $(this).val();
var dateReg = /^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/;
if(!dateReg.test(inputVal)) {
alert('invalid date format: ' + inputVal);
}
我不喜欢这个解决方案,所以如果您能提出更好的方法,请发表评论。
I found a very useful regular expression for testing format and content of a date field in a regex example site
BUT I get a validation when I put in dates older than 2000 and since this is a field for inputting date of birth you can see why it would be a problem. I am sure it is an easy fix but regular expressions intimidate me.
$('#txtDOB').blur(function() {
//$('span.error-keyup-5').remove();
var inputVal = $(this).val();
var dateReg = /^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/;
if(!dateReg.test(inputVal)) {
alert('invalid date format: ' + inputVal);
}
I am not married to this solution so if you can suggest a better way please comment away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议不要测试字符串是否与您认为可能是好的日期的一种或多种格式匹配,而是询问 JavaScript 是否认为它是有效日期:
这假设您将接受用户以多种格式提供给您的内容(例如可怕的 US MM/DD/YYYY 或更正常的 ISO8601 YYYY-MM-DD)。相反,如果你有一个特定的格式,你只会接受,然后根据该格式解析你的字符串,取出年/月/日期,然后询问 JavaScript 这是否是一个有效的日期:
你需要检查年/月/日期全部匹配,因为
new Date(2011,11,32)
被接受并解释为 2012-1-1。另请参阅:确保日期有效的 JavaScript 方法
Instead of testing if a string matches one or more formats that you think might be good dates, I would suggest instead asking JavaScript if it thinks it is a valid date:
This assumes that you're going to accept what the user gives you in any of a variety of formats (e.g. the horrid US MM/DD/YYYY or the more sane ISO8601 YYYY-MM-DD). If instead you have a specific format you will only accept, then parse your string based on that, pull out the year/month/date, and then ask JavaScript if this is a valid date:
You need to check that the year/month/date all match because
new Date(2011,11,32)
is accepted and interpreted as 2012-1-1.See also: Javascript method to ensure that a date is valid
那里一团糟。首先,消除所有的{1}。这仅仅意味着一个实例,这是完全多余的。此外,具有一个值的字符类与字符本身相同。因此,[1] 变为 1。
因此,我们得到:
这大概是 MM/DD/YYYY。但 YYYY 只是 199[0-9] 并且任何年份 > 2000年和< 9999。哇,这是一个日期范围!
作为基础,尝试:
这给出了 1000 - 2999 的年份范围。但正如 Tim 上面所说,如果您想要真正有效的日期,您应该使用特定的日期验证器。
There's a whole lot of mess there. First, eliminate all the {1}'s. That just means one instance, which is totally redundant. Also, a character class with one value is the same as the character itself. So, [1] becomes 1.
So, that leaves us with:
This is MM/DD/YYYY presumably. but the YYYY is just 199[0-9] and any year > 2000 and < 9999. Wow, that's a date range!
As a basic, try:
This gives a year range of 1000 - 2999. But as Tim said above, if you want really valid dates, you should use a specific date validator.
如果您需要将日期字符串解析为日期,那么我会查看这个库:
If you need to parse date strings into dates then I would check out this library: