javascript正则表达式,验证日期问题 [英] javascript regexp, validating date problem

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

问题描述

下面我有一些验证日期的代码:

 函数validateForm(){var errFound = 0;//var patt_date = new RegExp("^(((((19 | 20)(([02468] [048])|([13579] [26]))-02-29))|((209] [0-9])|(19 [0-9] [0-9]))-(((((0 [1-9])|(1 [0-2]))-((0 [1-9])|(1 \ d)|(2 [0-8])))|(((((0 [13578])|(1 [02]))-31)|(((0,3-9])|(1 [0-2]))-(29 | 30))))))$);var patt_date = new RegExp("^ [0-9] {4}-((((0 [13578] |(10 | 12))-(0 [1-9] | [1-2] [0-9]| 3 [0-1]))|(02-(0 [1-9] | [1-2] [0-9]))|((0 [469] | 11)-(0 [1-9] | [1-2] [0-9] | 30)))$);if(patt_date.test(document.getElementById("datefrom").value)== false){errFound = errFound +1; document.getElementById("datefrom").className ="error";}如果(errFound> 0)alert('请更正红色字段!');别的返回true;返回false;} 


上面的代码应使用YYYY-MM-DD格式,但无法验证日期,例如"2009-02-29"

注释的代码应该工作(//var patt_date = new RegExp ...),它可以捕获"2009-02-29"
当我放置无效数据并尝试对其进行纠正时,它破坏了验证,在我纠正它们后(尤其是在具有多个输入的表单上),它总是抱怨表单值有问题

也许有人可以修复当前的正则表达式?


编辑,我只想简单替换上面的正则表达式,就意味着要使用新的正则表达式模式,而不是用于验证日期的全新方法
作为参考,我只是从以下位置获取正则表达式模式:
http://www.regexlib.com/REDetails.aspx?regexp_id=694
http://www.regexlib.com/REDetails.aspx?regexp_id=933

2009-02-29 上进行了测试,第一个链接工作&第二个没有.再一次的问题是,只有第二个正则表达式没有在第一个正则表达式中检测到 2009-02-29 值无效(但是它破坏了我的代码?因此它一定有问题).

谢谢,
德尔斯

解决方案

不要使用正则表达式进行整个日期验证,这确实在推动正则表达式设计的局限性.我建议改用此程序:

  1. 根据正则表达式/^ \ d {4}-\ d {2}-\ d {2} $/
  2. 检查日期
  3. 使用 substr()提取年,月和日,并将其转换为整数
  4. 使用一些 if 语句验证整数.像这样:

如果(月== 2){如果(天== 29){if(年%4!= 0 ||年%100 == 0 &&年%400!= 0){//失败}}否则,如果(天> 28){//失败}}否则,如果(month == 4 || month == 6 || month == 9 || month == 11){如果(天> 30){//失败}}别的 {如果(天> 31){//失败}

(肯定可以更简洁地写出)或者,您可能可以使用Javascript的 Date 类执行此验证-您可能需要执行诸如解析日期,将其转换回字符串,并检查两个字符串是否相等.(我不是Java语言专家)

I have some code for validating date below:


function validateForm() {
var errFound = 0;       
//var patt_date = new RegExp("^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))$");
var patt_date = new RegExp("^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$");
if (patt_date.test(document.getElementById("datefrom").value) == false){errFound = errFound + 1;document.getElementById("datefrom").className = "error";}

if (errFound > 0)
    alert('Please correct red colored field!');
else
    return true;
return false;   
}


Above code should work with YYYY-MM-DD format, but fail to validate date such as "2009-02-29"

The commented code should work (//var patt_date = new RegExp...), it can catch "2009-02-29",
but it ruin the validation when i put invalid data and try to correct it, it keeps complain there something wrong with form value after i had correct them (especially on form with multiple input)

Maybe someone can fix the current regex?


Edited, what i want just a simple replacement for above regexp, mean a new regexp pattern not the whole new method to validate date
And for reference, i simply grab the regexp pattern from:
http://www.regexlib.com/REDetails.aspx?regexp_id=694 and
http://www.regexlib.com/REDetails.aspx?regexp_id=933

Tested with 2009-02-29, 1st link work & 2nd not. Again the problem was only the 2nd regexp didn't detect value 2009-02-29 as invalid while 1st can (but it ruin my code? so it's must be there something wrong with it).

Thanks,
Dels

解决方案

Don't do the whole date validation with a regular expression, that's really pushing the limits of what regexps were designed for. I would suggest this procedure instead:

  1. Check date against regexp /^\d{4}-\d{2}-\d{2}$/
  2. Extract year, month, and day using substr() and convert to integers
  3. Use some if statements to validate the integers. Like so:

    if (month == 2) {
        if (day == 29) {
            if (year % 4 != 0 || year % 100 == 0 && year % 400 != 0) {
                // fail
            }
        }
        else if (day > 28) {
            // fail
        }
    }
    else if (month == 4 || month == 6 || month  == 9 || month == 11) {
        if (day > 30) {
            // fail
        }
    }
    else {
        if (day > 31) {
            // fail
    }

(That could certainly be written more concisely) Alternatively, you could probably perform this validation using Javascript's Date class - you might have to do something like parsing the date, converting it back to a string, and checking if the two strings are equal. (I'm not a Javascript expert)

这篇关于javascript正则表达式,验证日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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