2010-12-03 6 views
1

Voici la fonction pour valider la date. Le devrait être entre Aujourd'hui - 15 et Aujourd'hui. Quelqu'un peut-il refactoriser ce code.JavaScript pour valider la date

phpdatetoday est une chaîne sous la forme 2010, déc 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

Si ce code fonctionne, laissez-le tel quel. S'il y a des problèmes, posez des questions sur des problèmes spécifiques. Pourquoi voulez-vous refactoriser cela? – Kamarey

Répondre

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);} 

refondus! (?)

+0

+1 belle réponse, peu obfusquée, mais agréable xDD – SubniC

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*/); 
} 

) (méthode Date.setHours fixeront les heures/minutes/secondes à zéro et retourne des millisecondes depuis le 1er janvier 1970 UTC. Date.parse() renverra la date analysée sinon, si elle ne peut pas le faire, elle renverra NaN

Vous pouvez utiliser isNan() pour déterminer si la valeur d'une variable est un nombre ou non. Si 'inputDate' est NaN, vous pouvez avertir l'utilisateur que la date d'entrée est invalide.