2011-04-25 16 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 
} 
}