2010-12-03 199 views
1

以下是驗證日期的函數。應該在今天 - 15今天。有人可以重構此代碼。JavaScript驗證日期

phpdatetoday爲以下形式的字符串2010年,月,3

function validate(page, phpdatetoday) 
{ 
    var i = 0; 
    var fields = new Array(); 
    var fieldname = new Array(); 

    var day = document.getElementById('date_of_inspection_day').value; 
    var month = document.getElementById('date_of_inspection_month').value; 
    var year = document.getElementById('date_of_inspection_year').value; 
    var datesubmitted = new Date(year,month-1,day); 

    var daysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31); 

    if(month.length<1) 
    { 
     alert("Please enter a valid month"); 
     return false; 
    } 
    if(year.length != 4) 
    { 
     alert("Please enter a valid year"); 
     return false; 
    } 

    if (day.length<1 || day > daysInMonth[month-1] || month == 2 && year%4 != 0 && day >28) 
    { 
     alert("Please enter a valid day"); 
     return false; 
    } 

    var dateToday = new Date(phpdatetoday); 
    var day15  = dateToday.getDate()-15; // 15 days old 
    var month15 = dateToday.getMonth(); 
    var year15  = dateToday.getFullYear(); 

    if(day15 < 0 && month15 ==1) 
    { 
     month15 = 12; 
     year15 = year15-1; 
    } 
    else if(day15 < 0 && month15 !=1) 
    { 
     month15 = month15-1; 
    } 

    day15 = daysInMonth[month15-1] + day15; 

    var date15DayOld = new Date(year15,month15,day15); 

    if(date15DayOld > datesubmitted) 
    { 
     alert("Your date is older than 15 days"); 
    } 

    else if(datetoday < datesubmitted) 
    { 
     alert("invalid Date"); 
    } 
} 
+2

如果此代碼有效,請保持原樣。如果有問題,請詢問具體問題。你爲什麼要重構這個? – Kamarey 2010-12-03 09:18:15

回答

1
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('C K(L,w){3 i=0;3 H=b q();3 I=b q();3 c=k.t(\'J\').s;3 9=k.t(\'G\').s;3 d=k.t(\'A\').s;3 u=b j(d,9-1,c);3 l=b q(7,E,7,g,7,g,7,7,g,7,g,7);6(9.n<1){e("v r a p 9");m o}6(d.n!=4){e("v r a p d");m o}6(c.n<1||c>l[9-1]||9==2&&d%4!=0&&c>R){e("v r a p c");m o}3 f=b j(w);3 8=f.N()-y;3 5=f.Q();3 h=f.P();6(8<0&&5==1){5=O;h=h-1}x 6(8<0&&5!=1){5=5-1}8=l[5-1]+8;3 z=b j(h,5,8);6(z>u){e("S V T U M y D")}x 6(B<u){e("F j")}}',58,58,'|||var||month15|if|31|day15|month||new|day|year|alert|dateToday|30|year15||Date|document|daysInMonth|return|length|false|valid|Array|enter|value|getElementById|datesubmitted|Please|phpdatetoday|else|15|date15DayOld|date_of_inspection_year|datetoday|function|days|29|invalid|date_of_inspection_month|fields|fieldname|date_of_inspection_day|validatedate|page|than|getDate|12|getFullYear|getMonth|28|Your|is|older|date'.split('|'),0,{})) 
function validate(page, phpdatetoday){return validatedate(page, phpdatetoday);} 

重構!(?)

+0

+1不錯的答案,有點模糊,但不錯的xDD – SubniC 2010-12-03 11:23:21

1
function validate(phpdatetoday, withinDays) { 
    var inputDateInMillis = Date.parse(phpdatetoday) 

    if (isNaN(inputDate) || isNaN(withinDays)) { 
     //handle error 
     return; 
    } 

    var todayInMillis = (new Date()).setHours(0,0,0,0); 
    return todayInMillis - inputDateInMillis < (withinDays * 86400000 /*1000ms*60s*60m*24h*/); 
} 

Date.setHours()將設置小時/分鐘/秒至零,並自1970年1月1日起返回毫秒。

Date.parse()將返回解析的日期,否則如果它不能這樣做,那麼它將返回NaN。您可以使用isNan()來確定變量的值是否爲數字。如果'inputDate'是NaN,那麼你可以提醒用戶輸入日期無效。