Forum de discussion
Forum « Programmation Javascript » (archives)
Probleme dans mon code javascript
Envoyé: 22 septembre 2003, 16h38 par gecko
j'ai une fonction de validation de date qui semble causé probleme sur certains navigateur. En effet, avec Netscape 7.02 MAC et la version récente d'IE version PC, aucun probleme. Mais si je roule la fonction avec Safari ou IE MAC, ca ne marche pas et aucun message d'erreur... Voici mon code:
function isDate (objname, DateField,msg) {
if(DateField.charAt(1) == '/')
DateField = '0' + DateField
if(DateField.charAt(4) == '/')
DateField = DateField.substring(0,3) + '0' + DateField.substring(3,9)
    var ValidDigits = "0123456789/";
    var allNum = "";
    var InvalidDigit = "";
    var mmddccyy = DateField.replace(/ /gi,"");
    var month = mmddccyy.substring(0,2);
    var day   = mmddccyy.substring(3,5);
    var year  = mmddccyy.substring(6,10);
    var today = new Date();
    month = ((!month) ? today.getMonth():month-1);
    var test = new Date(year,month,day);
    for (i = 0;  i < DateField.length;  i++)
       {
        ch = DateField.charAt(i);
        for (j = 0;  j < ValidDigits.length;  j++)
           {
            if (ch == ValidDigits.charAt(j))
              {
               break;
              }
           }
        if (j == ValidDigits.length)
          {
           InvalidDigit = 'YES';
           break;
          }
       }
    if (DateField.length == 8 && DateField / DateField == 1)
      {
       mmddccyy = DateField.substring(0,2);
       mmddccyy += '/';
       mmddccyy += DateField.substring(2,4);
       mmddccyy += '/';
       mmddccyy += DateField.substring(4,8);
       mmddccyy = mmddccyy.replace(/ /gi,"");
       month = mmddccyy.substring(0,2);
       day   = mmddccyy.substring(3,5);
       year  = mmddccyy.substring(6,10);
       month = ((!month) ? today.getMonth():month-1);
       var test = new Date(year,month,day);
      }
    if (mmddccyy.length == 0)
      {
        return mmddccyy;
      }
    else
    if (InvalidDigit  == 'YES')
      {
       alert("Invalid "+msg+" : " + DateField + "\nPlease Enter Valid Date : Format MM/DD/CCYY");
       objname.focus();
       return "";
      }
    else
    if (mmddccyy / mmddccyy == 1 || year.length != 4)
      {
        alert("Invalid "+msg+" : " + DateField + "\nPlease Enter Valid Date : Format MM/DD/CCYY");
        objname.focus();
        return "";
      }
    else
    if ( (month == test.getMonth()) && (day == test.getDate()) )
      {
        return mmddccyy;
      }
    else
      {
        alert("Invalid "+msg+" : " + DateField + "\nPlease Enter Valid Date : Format MM/DD/YYYY");
        objname.focus();
        return "";
      }
}
Réponses
|