Link to home
Start Free TrialLog in
Avatar of TrueBlue
TrueBlueFlag for United States of America

asked on

checkdate function giving undefined error

Hi!

I am calling the following javascript via the following two statements on an xsl page:

<td align="center">
            <input maxLength="8" name="startdt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
           </td>
          <td align="center">
            <input maxLength="8" name="enddt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
           </td>

I am getting the following error when I am leaving the start date field whether or not a value is entered:

startdt is undefined

Any ideas?

Chuck

 var dosubmit = true;

    function checkDates()
    {

          var sStartDate;
          var sEndDate;

           if (!checkDate(startdt)) return false;
           if (!checkDate(enddt)) return false;

          sStartDate = startdt.value;
          sEndDate = enddt.value;

           dtStartDate = ConvertStringToDate(startdt.value);
           dtEndDate = ConvertStringToDate(sEndDate);

           if (dtStartDate > dtEndDate)
           {
                alert('The end date (' + sEndDate + ') is earlier than the start date (' + sStartDate + ')');
                return false;
           }

           return true;

     }

     function ConvertStringToDate(sDate)
     {

          var sDateArray;
          var iMonth;
          var iDay;
          var iYear;

          if (sDate.indexOf('/') != -1)
           {
                sDateArray = sDate.split('/');
               iMonth = parseInt(sDateArray[0], 10);
               iDay = parseInt(sDateArray[1], 10);
               iYear = parseInt(sDateArray[2], 10);
           }

          return new Date(iYear, iMonth, iDay);

     }


   function checkDate(oDateElem, sDate)
   {


             var sDate = oDateElem.value;

             if (sDate.length < 1)
           {
                return true;
           }

           if (sDate.indexOf('/') != -1)
           {
                sDateArray = sDate.split('/');
                if (sDateArray.length != 3)
               {
                    alert('Invalid date (' + sDate + ').');
                    oDateElem.focus();
                    return false;
               }
                sMonth = sDateArray[0];
                sDay = sDateArray[1];
                sYear = sDateArray[2];
           }

           if (sYear.length == 1)
           {
                sYear = '200' + sYear;
           }
           if (sYear.length == 2)
           {
                sYear = '20' + sYear;
           }

           iDay = parseInt(sDay, 10);

           if (isNaN(iDay))
           {
               alert('Invalid date (' + sDate + ').');
               oDateElem.focus();
               return false;
           }

           iMonth = parseInt(sMonth, 10);
           if (isNaN(iMonth))
           {
               alert('Invalid date (' + sDate + ').');
               oDateElem.focus();
               return false;
           }

           iYear = parseInt(sYear, 10);
           if (isNaN(iYear))
           {
               alert('Invalid date (' + sDate + ').');
               oDateElem.focus();
               return false;
           }

           if (iYear < 1900 || iYear > 2078)
           {
               alert('Invalid date - year must be between 1900 and 2078 (' + sDate + ').');
               oDateElem.focus();
               return false;
           }


           if (iMonth > 12 || iMonth < 1)
           {
               alert('Invalid date - month must be between 1 and 12 (' + sDate + ').');
               oDateElem.focus();
               return false;
           }

           if ((iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 || iMonth == 8 || iMonth == 10 || iMonth == 12) && (iDay > 31 || iDay < 1))
           {
               alert('Invalid date - day must be between 1 and 31 (' + sDate + ').');
               oDateElem.focus();
               return false;
           }

           if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && (iDay > 30 || iDay < 1))
           {
               alert('Invalid date - day must be between 1 and 30 (' + sDate + ').');
               oDateElem.focus();
               return false;
           }

           if (iMonth == 2)
           {
                if (LeapYear(iYear) == true)
                {
                     if (iDay < 1 || iDay > 29)
                     {
                         alert('Invalid date - day must be between 1 and 29 (' + sDate + ').');
                         oDateElem.focus();
                         return false;
                     }
                }
                else
                {
                     if (iDay < 1 || iDay > 28)
                     {
                         alert('Invalid date - day must be between 1 and 28 (' + sDate + ').');
                         oDateElem.focus();
                         return false;
                     }
                }
           }

          oDateElem.value = iMonth + "/" + iDay + "/" + iYear;

           return true;
     }


     function LeapYear(iYear)
     {
          if (iYear % 100 == 0)
          {
               if (iYear % 400 == 0)
               {
                    return true;
               }
          }
          else
          {
               if ((iYear % 4) == 0)
               {
                    return true;
               }
          }
          return false;
     }
Avatar of ho_alan
ho_alan

um...
1.u can use a form
<form name=fm1>
<td align="center">
           <input maxLength="8" name="startdt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td>
         <td align="center">
           <input maxLength="8" name="enddt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td>
</form>

then,  in the function
         sStartDate = document.fm1.startdt.value;
         sEndDate = document.fm1.enddt.value;
----------------------------------------------------------------------------------------
or

           <input maxLength="8" id="startdt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td>
         <td align="center">
           <input maxLength="8" id="enddt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td>

then,  in the function
         sStartDate = document.all.startdt.value;
         sEndDate = document.all.enddt.value;

hope this helps



because "startdt"  is the name  of the input field
but a JS variable
Avatar of TrueBlue

ASKER

Hi ho_alan!

I used your second method, but unfortunately I get the exact same error.

If I understood correctly, you wanted me to substitue this:
 sStartDate = startdt.value;
 sEndDate = enddt.value;

with this:

 sStartDate = document.all.startdt.value;
 sEndDate = document.all.enddt.value;
 
Any other ideas?

Chuck
um...u should  change

all  "startdt" and "enddt"
to be "document.all.startdt" and "document.all.enddt"

eg.
   if (!checkDate(document.all.startdt)) return false;
          if (!checkDate(document.all.enddt)) return false;

         sStartDate = document.all.startdt.value;
         sEndDate = document.all.enddt.value;

hope this  helps
i have tried,should work  without error

<html>
<body>
<script>
var dosubmit = true;

   function checkDates()
   {

         var sStartDate= document.all.startdt;
         var sEndDate= document.all.enddt;

          if (!checkDate(sStartDate))  return false;
          if (!checkDate(sEndDate)) ; return false;


          dtStartDate = ConvertStringToDate(document.all.startdt.value);
          dtEndDate = ConvertStringToDate(sEndDate);

          if (dtStartDate > dtEndDate)
          {
               alert('The end date (' + sEndDate + ') is earlier than the start date (' + sStartDate + ')');
               return false;
          }

          return true;

    }

    function ConvertStringToDate(sDate)
    {

         var sDateArray;
         var iMonth;
         var iDay;
         var iYear;

         if (sDate.indexOf('/') != -1)
          {
               sDateArray = sDate.split('/');
              iMonth = parseInt(sDateArray[0], 10);
              iDay = parseInt(sDateArray[1], 10);
              iYear = parseInt(sDateArray[2], 10);
          }

         return new Date(iYear, iMonth, iDay);

    }


  function checkDate(oDateElem)
  {


            var sDate = oDateElem.value;

            if (sDate.length < 1)
          {
               return true;
          }

          if (sDate.indexOf('/') != -1)
          {
               sDateArray = sDate.split('/');
               if (sDateArray.length != 3)
              {
                   alert('Invalid date (' + sDate + ').');
                   oDateElem.focus();
                   return false;
              }
               sMonth = sDateArray[0];
               sDay = sDateArray[1];
               sYear = sDateArray[2];
          }

          if (sYear.length == 1)
          {
               sYear = '200' + sYear;
          }
          if (sYear.length == 2)
          {
               sYear = '20' + sYear;
          }

          iDay = parseInt(sDay, 10);

          if (isNaN(iDay))
          {
              alert('Invalid date (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          iMonth = parseInt(sMonth, 10);
          if (isNaN(iMonth))
          {
              alert('Invalid date (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          iYear = parseInt(sYear, 10);
          if (isNaN(iYear))
          {
              alert('Invalid date (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if (iYear < 1900 || iYear > 2078)
          {
              alert('Invalid date - year must be between 1900 and 2078 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }


          if (iMonth > 12 || iMonth < 1)
          {
              alert('Invalid date - month must be between 1 and 12 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if ((iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 || iMonth == 8 || iMonth == 10 || iMonth == 12) && (iDay > 31 || iDay < 1))
          {
              alert('Invalid date - day must be between 1 and 31 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && (iDay > 30 || iDay < 1))
          {
              alert('Invalid date - day must be between 1 and 30 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if (iMonth == 2)
          {
               if (LeapYear(iYear) == true)
               {
                    if (iDay < 1 || iDay > 29)
                    {
                        alert('Invalid date - day must be between 1 and 29 (' + sDate + ').');
                        oDateElem.focus();
                        return false;
                    }
               }
               else
               {
                    if (iDay < 1 || iDay > 28)
                    {
                        alert('Invalid date - day must be between 1 and 28 (' + sDate + ').');
                        oDateElem.focus();
                        return false;
                    }
               }
          }

         oDateElem.value = iMonth + "/" + iDay + "/" + iYear;

          return true;
    }


    function LeapYear(iYear)
    {
         if (iYear % 100 == 0)
         {
              if (iYear % 400 == 0)
              {
                   return true;
              }
         }
         else
         {
              if ((iYear % 4) == 0)
              {
                   return true;
              }
         }
         return false;
    }

</script>
<td align="center">
           <input maxLength="8" id="startdt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td>
         <td align="center">
           <input maxLength="8" id="enddt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td
</body>
</html>
no ";",i mistyped it
         if (!checkDate(sEndDate))  return false;

please tell if u still have problems  :-)
please wait
i am  making some changes for u

is that if user enters "1"
then it will be converted to "1/1/2000"?
ok, i got it
please wait for my final version
<html>
<body>
<script language=javascript>
var dosubmit = true;

   function checkDates()
   {

         var sStartDate= document.all.startdt;
         var sEndDate= document.all.enddt;

          if (!checkDate(sStartDate))  return false;
          if (!checkDate(sEndDate))  return false;


          dtStartDate = ConvertStringToDate(sStartDate.value);
          dtEndDate = ConvertStringToDate(sEndDate.value);

          if (dtStartDate > dtEndDate)
          {
               alert('The end date (' + sEndDate.value + ') is earlier than the start date (' + sStartDate.value + ')');
               return false;
          }

          return true;

    }

    function ConvertStringToDate(sDate)
    {

         var sDateArray;
         var iMonth;
         var iDay;
         var iYear;

         if (sDate.indexOf('/') != -1)
          {
               sDateArray = sDate.split('/');
              iMonth = parseInt(sDateArray[0],10);
              iDay = parseInt(sDateArray[1],10);
              iYear = parseInt(sDateArray[2],10);
          }

         return new Date(iYear, iMonth, iDay);

    }


  function checkDate(oDateElem)
  {

           var sDate = oDateElem.value;
         if (sDate == "") return true;
      
           if (sDate.length < 1)
      
          {
               return true;
          }

          if (sDate.indexOf('/') != -1)
          {
               sDateArray = sDate.split('/');
               sMonth = sDateArray[0];
               sDay = sDateArray[1];
               sYear = sDateArray[2];
          }
        else
        {
                alert('Invalid date (' + sDate + ').');
                oDateElem.focus();
                return false;
        }
         


          if (sYear.length == 1)
          {
               sYear = '200' + sYear;
          }
          if (sYear.length == 2)
          {
               sYear = '20' + sYear;
          }

          iDay = parseInt(sDay, 10);

          if (isNaN(iDay))
          {
              alert('Invalid date (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          iMonth = parseInt(sMonth, 10);
          if (isNaN(iMonth))
          {
              alert('Invalid date (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          iYear = parseInt(sYear, 10);
          if (isNaN(iYear))
          {
              alert('Invalid date (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if (iYear < 1900 || iYear > 2078)
          {
              alert('Invalid date - year must be between 1900 and 2078 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }


          if (iMonth > 12 || iMonth < 1)
          {
              alert('Invalid date - month must be between 1 and 12 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if ((iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 || iMonth == 8 || iMonth == 10 || iMonth == 12) && (iDay > 31 || iDay < 1))
          {
              alert('Invalid date - day must be between 1 and 31 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && (iDay > 30 || iDay < 1))
          {
              alert('Invalid date - day must be between 1 and 30 (' + sDate + ').');
              oDateElem.focus();
              return false;
          }

          if (iMonth == 2)
          {
               if (LeapYear(iYear) == true)
               {
                    if (iDay < 1 || iDay > 29)
                    {
                        alert('Invalid date - day must be between 1 and 29 (' + sDate + ').');
                        oDateElem.focus();
                        return false;
                    }
               }
               else
               {
                    if (iDay < 1 || iDay > 28)
                    {
                        alert('Invalid date - day must be between 1 and 28 (' + sDate + ').');
                        oDateElem.focus();
                        return false;
                    }
               }
          }

         oDateElem.value = iMonth + "/" + iDay + "/" + iYear;

          return true;
    }


    function LeapYear(iYear)
    {
         if (iYear % 100 == 0)
         {
              if (iYear % 400 == 0)
              {
                   return true;
              }
         }
         else
         {
              if ((iYear % 4) == 0)
              {
                   return true;
              }
         }
         return false;
    }

</script>
<td align="center">start
           <input maxLength="8" id="startdt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td>
         <td align="center">end
           <input maxLength="8" id="enddt" size="8" onfocus="dosubmit=false;" onblur="dosubmit=checkDates();" value=""/>
          </td>
</body>
</html>
How about this:


<html>
<head>
<script>

 var dosubmit = true;

    function checkDates(theField)
    {
          var theForm = theField.form;
          var sStartDate;
          var sEndDate;

           if (!checkDate(theForm.startdt)) return false;
           if (!checkDate(theForm.enddt)) return false;

          sStartDate = theForm.startdt.value;
          sEndDate = theForm.enddt.value;

           dtStartDate = ConvertStringToDate(theForm.startdt.value);
           dtEndDate = ConvertStringToDate(sEndDate);

           if (dtStartDate > dtEndDate)
           {
                alert('The end date (' + sEndDate + ') is earlier than the start date (' + sStartDate + ')');
                return false;
           }

           return true;

     }

     function ConvertStringToDate(sDate)  {

          var sDateArray;
          var iMonth;
          var iDay;
          var iYear;

          if (sDate.indexOf('/') != -1)
           {
                sDateArray = sDate.split('/');
               iMonth = parseInt(sDateArray[0], 10);
               iDay = parseInt(sDateArray[1], 10);
               iYear = parseInt(sDateArray[2], 10);
           }

          return new Date(iYear, iMonth, iDay);

     }


   function checkDate(oDateElem)  {

     var sDate = oDateElem.value.split("/");
     if (sDate.length != 3) {
        return false;
     }
     iDate = new Date(oDateElem.value);
     iMonth = iDate.getMonth()+1;
     iDay = iDate.getDate();
     iYear = iDate.getFullYear();
     if(sDate[0]!=iMonth || sDate[1]!=iDay || sDate[2]!=iYear){
       alert('Invalid date.');
       oDateElem.select();
       oDateElem.focus();
       return false;
     }
     return true;
   }


</script>
</head>
<body>
<form>
<table><tr>
<td align="center">
            <input maxLength="10" name="startdt" size="10" onfocus="dosubmit=false;" onblur="dosubmit=checkDates(this);" value=""/>
           </td>
          <td align="center">
            <input maxLength="10" name="enddt" size="10" onfocus="dosubmit=false;" onblur="dosubmit=checkDates(this);" value=""/>
           </td>
</tr></table>
</form>
</body>
</html>



The problem might be that your function is located before the input field is created?

If so - try moving the function that deals with "strtdt" to the end of your code..
Avatar of Michel Plungjan
Please note that NetGrooves code will work in more browsers since it is passing the form/form field instead of usin IE only document.all
Ofeke: You can place the function anywhere as long as it is not CALLED before the form exists
Nope - some browsers perform "precheck" for objects. even IE do that in some cases.

Not only when you call a function - also when you cause an event, eval object etc etc
Not in my universe....
Hi Ho_Alan!

I get the following error with the new version whether I enter a value or not.

Length is null or not an object

Chuck
Hi Netgroove!

I get the same error length is null or not an object with your version as well.

Any ideas?

Chuck
Hi Netgroove!

I was wrong I get value is null or not an object from your suggestion.

Thank you in advance.

Chuck
Check this:

<html>
<head>
<script>
 var dosubmit = false;

    function checkDates(theField)
    {
          var theForm = theField.form;
          var sStartDate;
          var sEndDate;

           if (!checkDate(theForm.startdt)) return false;
           if (!checkDate(theForm.enddt)) return false;

          sStartDate = theForm.startdt.value;
          sEndDate = theForm.enddt.value;

           dtStartDate = ConvertStringToDate(theForm.startdt.value);
           dtEndDate = ConvertStringToDate(sEndDate);

           if (dtStartDate > dtEndDate)
           {
                alert('The end date (' + sEndDate + ') is earlier than the start date (' + sStartDate + ')');
                return false;
           }

           return true;

     }

     function ConvertStringToDate(sDate)  {

          var sDateArray;
          var iMonth;
          var iDay;
          var iYear;

          if (sDate.indexOf('/') != -1)
           {
                sDateArray = sDate.split('/');
               iMonth = parseInt(sDateArray[0], 10);
               iDay = parseInt(sDateArray[1], 10);
               iYear = parseInt(sDateArray[2], 10);
           }

          return new Date(iYear, iMonth, iDay);

     }


   function checkDate(oDateElem)  {

     var sDate = oDateElem.value.split("/");
     if (sDate.length != 3) {
        if(oDateElem.value>""){
          alert("Inavlid Date format: "+oDateElem.value+"\nEnter Date in this format: MM/DD/YYYY");
          oDateElem.select();
          oDateElem.focus();
        }
        return false;
     }
     iDate = new Date(oDateElem.value);
     iMonth = iDate.getMonth()+1;
     iDay = iDate.getDate();
     iYear = iDate.getFullYear();
     if(sDate[0]!=iMonth || sDate[1]!=iDay || sDate[2]!=iYear){
       alert('Invalid date.');
       oDateElem.select();
       oDateElem.focus();
       return false;
     }
     return true;
   }
</script>
</head>
<body>
<form onSubmit="return dosubmit">
<table><tr>
<td align="center">
            <input maxLength="10" name="startdt" size="10" onfocus="dosubmit=false;" onblur="dosubmit=checkDates(this);" value=""/>
           </td>
          <td align="center">
            <input maxLength="10" name="enddt" size="10" onfocus="dosubmit=false;" onblur="dosubmit=checkDates(this);" value=""/>
           </td>
</tr></table>
<input type=submit>
</form>
</body>
</html>


Also is your error message not comming from my example.
Some additional code contains some typo.
Could you post your complete code for debugging?

Hi NetGroove!

I am sure the error is coming from this function because if I substitute:

       <input maxLength="10" name="enddt" size="10" onfocus="dosubmit=false;" onblur="dosubmit=checkDates(this);" value=""/>

with

       <input maxLength="10" name="enddt" size="10" value=""/>

I get no errors.

Here is my submission button in xslt:

  <xsl:if test="$aspemail != 'true'">
            <input type="submit" NAME="sendChanges" VALUE="Only Press Once to Send Changes">
              <xsl:attribute name="onclick"><![CDATA[ if(doSubmit) { document.form1.submit(); } ]]></xsl:attribute>
            </input>
          </xsl:if>

Here is my form code in asp:

<HTML>
  <head>
    <title>Call List Table</title>
   
     <script language="Javascript" type="text/javascript" src="datemask.js"></script>

  </head>
<body>
 <form name="form1" onsubmit="if(doSubmit) { return true; } else { return false; }" ACTION="calllist.asp" METHOD="POST">
   <input type="hidden" id="hiddeninput" value="">
   <input type="hidden" name="itemToDelete" value="">
   <input type="hidden" name=a value="<%=Request("a")%>">

Would I need to replace the phrase "theform" in your code with "form1" since that is what my form is named?

I did not include the opening / closing script tags when I copied this code to the datemask.js file.

Chuck
The var dosubmit has to be global defined.
And the spelling has to be always the same, not doSubmit

You get an error for this event:
  onfocus="dosubmit=false;"

Because dosubmit is not previously defined.

In xslt you even name it doSubmit

Change all vars to same name.

Also the same for theForm. You can not rename it to theform or form1.
My proposal get the form reference trough the input field reference passed by reference to itself: this

Hi NetGroove:

I did a search and replace through my asp, xsl and js pages. The variable is now doSubmit everywhere, but I am still get the same error value is null or not an object.

Is there a way to stop the script at certain points to see where the value problem is?

Similiar to response.write sStartdt
               response.end

Thank you for you help.

Chuck
where is onSubmit origianly defined and initialized?
I supos it has to be defined in datemask.js

If it is nowhere initialized, then you get an error for compare in "if(doSubmit)"

Hi NetGroove!

I have another javascript function that checks the telephone field's format.
It is initialized I believe in there.

var doSubmit = true;

It is called by a similiar statement on the xsl page as well.

<INPUT maxLength="12" name="priphone" size="12" onfocus="doSubmit=false;"
             onblur="checkPhone(this);" onkeyup="phoneMask(this);" onkeydown="phoneMask(this);"
             value=""/>

Chuck


SOLUTION
Avatar of ho_alan
ho_alan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please do not give grading B for such long running support.

Anyway, thanks for the points.
You do not know, but eperts get more points when you grade A.
If you want to do me a favor, then let correct your grading by posting a request in CS topic area:
https://www.experts-exchange.com/Community_Support/

Cheers,
NetGroove

Hi NetGroove!

I appreciate your help on this issue, but I was never able to get it to work.

So I just closed the question.

Chuck
Sorry for that.
Ok, hope next time to be of more help.

See you,
NetGroove