2011-04-25 46 views
0

出于好奇,有没有人会知道如何简化这个过程。 我正在写一个日期验证程序的正则表达式,它只接受mm.dd.yyyy mm/dd/yyyy或mm-dd-yyyy格式。我认为这种方式很有效,但看起来真的很好。如何简化在JavaScript中检查和报告有效日期?

function c(x, y) 
{ 
//check that there is a date and in right order 
if ((x==1) || (x==3)) 
{ 
    if(y == "") 
    {alert("You have not entered a date"); 
    return false; 
    } 
    var string = document.getElementById('date'); 
    var w = string.value.search(/^(\d{2})([ ./-]{1})(\d{2})\2(\d{4})$/); 
    if (w != 0) 
     { 
    alert("Bad Date pattern match please redo"); 
    return false; 
    } 
    var patt=/\d{2}/ 
    var result=patt.exec(string.value); 
    if(result > 12) 
    { 
    alert("Please redo Date(Month)"); 
     return false; 
    } 
    patt2=/([ ./-])\d{2}\1/ 
    result=patt2.exec(string.value); 
    result=patt.exec(result); 
    if(result > 31) 
    { 
    alert("Please redo Date(Days)"); 
    return false; 
    } 
    patt=/([ ./-])\d{4}/ 
    result=patt.exec(string.value); 
    patt2=/\d{4}/ 
    result=patt2.exec(result); 
    if(result > 2011) 
    { 
     alert("Please redo Date(Years)"); 
     return false; 
    }  
} 
+0

如果你想要细粒度的错误报告,你需要那种代码:) – alex 2011-04-25 05:47:58

+0

@alex:澄清吗? – willis0924 2011-04-25 05:50:44

回答

0
//The OP asked for a simpler way, and here it is: 
function validateDate(dateValue) 
{ 
    //improving the regular expression to do even more wonders 
    //is left as an exercise :-) 
    var pattern = /^(0[1-9]|1[012])[/.-](0[1-9]|[12][0-9]|3[01])[/.-](19|20)\d\d$/ 
    if(pattern.test(dateValue) == true) 
    alert('valid date'); 
    else 
    alert('invalid date'); 
} 
//OK, I don't have lots of the fine-grained error messages in, 
//but all 3 formats should pass this 
0

你看看Regular Expression Matching a Valid Date

下面是解析mm/dd/yyyy的正则表达式示例。

^(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d$

您可以更改分隔符来解析mm.dd.yyyymm-dd-yyyy

+0

OP已经与日期匹配。 – alex 2011-04-25 05:51:32

+0

@alex我只是给他一个关于他可以做同样事情的暗示,尽管我同意他在他的问题下的评论:如果他需要告诉用户什么写得不好,他将不得不做一些类似因为如果日期不正确,我的答案中的正则表达式不会匹配,除了一年之外,他需要检查最后匹配的组是否比'2011'更大。 – 2011-04-25 06:02:19

0

您发布的代码对我的眼睛是痛苦的。请住手。

x = 'mm-dd-yyyy' 

xArray = x.split('-') 

for(var i = 0; i < 3; i++) 
    (isNumeric(xArray[0])) ? null : return false 

该代码与其他分隔符相同,只是检查哪个分隔符存在。你可能需要详细说明这一点,以获得所有期望的断言,但它会告诉你如何。

正则表达式是不好意思尽量避免它们。

+1

比添加全局变量和丢失分号更加痛苦吗? :P – alex 2011-04-25 05:52:29

+0

上下文不包括:/是的,我知道自动分号插入是如何工作的,所以我选择在易读性的地方忽略它们。事实上,我将删除其他人。 – GAgnew 2011-04-25 05:54:25

0

正则表达式必须是一个怪物捕捉任何不正确的输入,像02月29日,2011年

简单的就是尽量使输入的日期,然后检查,以确保2/29没有评估到3/1。

function isvalid_mdy(s){ 
    var day, A= s.split(/\D+/); 
    A[0]= parseInt(A[0], 10)-1; 
    A[1]= parseInt(A[1], 10); 
    A[2]= parseInt(A[2], 10); 
    try{ 
     day= new Date(A[2], A[0], A[1]); 
     if(day.getMonth()== A[0] && day.getDate()== A[1]) return day; 
     throw new Error('Bad Date '+s); 
    } 
    catch(er){ 
     alert(er.message) 
     return NaN; 
    } 
} 
var s1= '04/31/2011'; 
isvalid_mdy(s1) 
0

This?

function c(a, b) { 
if (a == 1 || a == 3) { 
    if (b == "") return alert("You have not entered a date"), !1; 
    var c = document.getElementById("date"), 
     d = c.value.search(/^(\d{2})([ ./-]{1})(\d{2})\2(\d{4})$/); 
    if (d != 0) return alert("Bad Date pattern match please redo"), !1; 
    var e = /\d{2}/, 
     f = e.exec(c.value); 
    if (f > 12) return alert("Please redo Date(Month)"), !1; 
    if (patt2 = /([ ./-])\d{2}\1/, f = patt2.exec(c.value), f = e.exec(f), f > 31) return alert("Please redo Date(Days)"), !1; 
    if (e = /([ ./-])\d{4}/, f = e.exec(c.value), patt2 = /\d{4}/, f = patt2.exec(f), f > 2011) return alert("Please redo Date(Years)"), !1 
} 
}