views:

373

answers:

1

Hello there, peoples of web!

Maybe one of you will spot what's amiss in the following function: It is a javascript function used on a textbox client-side onblur event to validate that the entered text represents a correctly formatted and valid date.

I tried a few times, and it worked, but it seems ! should've tried all dates!

it will work for all valid combination but it can't seem to parse "08" and "09" which always parse as 0. I tried it in both IE and Firefox and the result is the same, and FF's javascript console doesn't find any syntax error in the code.

Anyone wanna try and outsmart me? I left alerts in the code to check my variables' values at critical points and everything seems fine to me, up to the parsing, if I have 08 or 09, in the day or month part...

The function used follows.

function DateVal(controle, culture, bToday)
{
    //var strDate = controle.value;
    var separateur = '';
    var dayPart = '';
    var monthPart = '';
    var yearPart = '';
    var tblParts;
    var isBisexuel = false;
    var valid = true;

    //alert('DateVal : Controle: ' + controle + 'Controle.value.length: ' + controle.value.length +  ' value: ' + controle.value  + ' culture: ' + culture);
    if (controle.value.length == 10)

    {
     if (culture == 'en-US') 
     {// Il s'agit d'un format de date avec séparateur "/", MM/dd/yyyy
      separateur = '/';
      dayPart = 1;
      monthPart = 0;
      yearPart = 2;
     }
     else if (culture == 'en-GB')
     {// Il s'agit d'un format de date avec séparateur "/", dd/MM/yyyy
      separateur = '/';
      dayPart = 0;
      monthPart = 1;
      yearPart = 2;
     }
     else if (culture == 'fr-CA')
     {// Il s'agit d'un format de date avec séparateur "-", yyyy-MM-dd
      separateur = '-';
      dayPart = 2;
      monthPart = 1;
      yearPart = 0;
     }
     else
     {
      valid = false;
      alert('Le contrôle est mal initialisé (pas de culture ou culture invalide); il n\'y a rien qui va marcher...');
      //window.event.returnValue =false;
     }
    }
    else
    {
     alert('longueur pas valide');

     valid = false;
    }
    if (valid == true)
    {
     alert('valeur: ' + controle.value + ' separateur: ' + separateur);
     tblParts = controle.value.split(separateur);
     alert('yavant parse: dayPart: ' + tblParts[dayPart] + 'monthPart: ' + tblParts[monthPart] +  ' yearPart: ' + tblParts[yearPart]);
     if (tblParts.length == 3)
     {
      dayPart = parseInt(tblParts[dayPart]);
      monthPart = parseInt(tblParts[monthPart]);
      yearPart = parseInt(tblParts[yearPart]);

      alert('après parse: pos0: ' + dayPart + 'pos1: ' + monthPart +  ' pos2: ' + yearPart);

      if (isNaN(dayPart) || isNaN(monthPart) || isNaN(yearPart))
      {
       //alert(dayPart + ' ' + monthPart + ' ' + yearPart);
       valid = false;
      }
      else
      {
       isBisexuel = ((yearPart % 400 == 0) || (yearPart % 4 == 0 && yearPart % 100 != 0));
       alert(isBisexuel);
       if (isBisexuel)
       {
        tblDays = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
       }
       else
       {
        tblDays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
       }

       if (monthPart <= 12 && monthPart > 0)
       {
        if (dayPart > tblDays[monthPart] || dayPart < 1)
        {
         alert('le jour fitte pas');
         valid = false;
        }
       }
       else
       {
        alert('mois égal à 0 ou moins, ou supérieur à 12');
        valid = false;
       }
      }
     }
     else
     {
      alert('le split donne pas 3 morceaux?');
      valid = false;
     }
    }
    //window.event.returnValue = valid;
    if(valid)
    {
     return;
    }
    else
    {
     if (bToday == true)
     {
      var dt = new Date();
      dayPart = dt.getDate();
      if (dayPart.toString().length == 1)
       dayPart = '0'+dayPart;
      monthPart = dt.getMonth();
      if (monthPart.toString().length == 1)
       monthPart = '0'+monthPart;
      yearPart = dt.getFullYear();
      //alert(dayPart + ' ' + monthPart + ' ' + yearPart + ' ' + culture);
      switch (culture)
      {
       case 'fr-CA':
        controle.value = yearPart + separateur + monthPart + separateur + dayPart;
        break;
       case 'en-US':
        controle.value = monthPart + separateur + dayPart + separateur + yearPart;
        break;
       case 'en-GB':
        controle.value = dayPart + separateur + monthPart + separateur + yearPart;
        break;
      }
      //alert(controle.value);
     }
     else
     {
      controle.value = '';
     }
    }
}

Thanks to all trying! Pascal

+3  A: 

You need to supply the radix parameter as 10 otherwise values starting with 0 are parsed as octal. For example:

dayPart = parseInt(tblParts[dayPart], 10);
Greg
Thank you very much!I wondered when i would've thought of that!
Pinpin
The radic parameter is one of the things Douglas Crockford's "JavaScript: The Good Parts" addresses. Also, using http://jslint.org on your code -- it will squawk about a missing radix parameter.
artlung