Solved

How do fix calendar date validation

Posted on 2013-12-13
23
281 Views
Last Modified: 2013-12-21
The popup calendar in a form is requiring a date within the current year. Is there something in this script that controls that? What would I change?

The js for the calendar itself is set 1999/2099

<SCRIPT type=text/javascript>
        <!--
        function collapseElem(obj)
        {
            var el = document.getElementById(obj);
            el.style.display = 'none';
        }

        function expandElem(obj)
        {
            var el = document.getElementById(obj);
            el.style.display = '';
        }

        //-->
        </SCRIPT>
<!-- expand/collapse function -->

<!-- expand/collapse function -->
<SCRIPT type=text/javascript>
            <!--

            // collapse all elements, except the first one
            function collapseAll()
            {
                var numFormPages = 3;

                for(i=2; i <= numFormPages; i++)
                {
                    currPageId = ('page_' + i);
                    collapseElem(currPageId);
                }
            }

            //-->
            </SCRIPT>
<!-- expand/collapse function -->
<!-- validate -->
<SCRIPT type=text/javascript>
<!--
function validateField(fieldId, fieldBoxId, fieldType, required)
            {
                fieldBox = document.getElementById(fieldBoxId);
                fieldObj = document.getElementById(fieldId);

                if(fieldType == 'text'  ||  fieldType == 'textarea'  ||  fieldType == 'password'  ||  fieldType == 'file'  ||  fieldType == 'phone'  || fieldType == 'website')
                {	
                    if(required == 1 && fieldObj.value == '')
                    {
                        fieldObj.setAttribute("class","mainFormError");
                        fieldObj.setAttribute("className","mainFormError");
                        fieldObj.focus();
                        return false;					
                    }

                }

                else if(fieldType == 'menu'  || fieldType == 'country'  || fieldType == 'state')
                {	
                    if(required == 1 && fieldObj.selectedIndex == 0)
                    {				
                        fieldObj.setAttribute("class","mainFormError");
                        fieldObj.setAttribute("className","mainFormError");
                        fieldObj.focus();
                        return false;					
                    }

                }

                else if(fieldType == 'email')
                {	
                    if((required == 1 && fieldObj.value=='')  ||  (fieldObj.value!=''  && !validate_email(fieldObj.value)))
                    {				
                        fieldObj.setAttribute("class","mainFormError");
                        fieldObj.setAttribute("className","mainFormError");
                        fieldObj.focus();
                        return false;					
                    }

                }

            }

            function validate_email(emailStr)
            {		
                apos=emailStr.indexOf("@");
                dotpos=emailStr.lastIndexOf(".");

                if (apos<1||dotpos-apos<2) 
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

            function validateDate(fieldId, fieldBoxId, fieldType, required,  minDateStr, maxDateStr)
            {
                retValue = true;

                fieldBox = document.getElementById(fieldBoxId);
                fieldObj = document.getElementById(fieldId);	
                dateStr = fieldObj.value;


                if(required == 0  && dateStr == '')
                {
                    return true;
                }


                if(dateStr.charAt(2) != '/'  || dateStr.charAt(5) != '/' || dateStr.length != 10)
                {
                    retValue = false;
                }	

                else	// format's okay; check max, min
                {
                    currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
                    //alert(currDays);

                    if(maxDateStr != '')
                    {
                        maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
                        //alert(maxDays);
                        if(currDays > maxDays)
                            retValue = false;
                    }

                    if(minDateStr != '')
                    {
                        minDays = parseInt(minDateStr.substr(0,2),10) + parseInt(minDateStr.substr(3,2),10)*30  + parseInt(minDateStr.substr(6,4),10)*365;
                        //alert(minDays);
                        if(currDays < minDays)
                            retValue = false;
                    }
                }

                if(retValue == false)
                {
                    fieldObj.setAttribute("class","mainFormError");
                    fieldObj.setAttribute("className","mainFormError");
                    fieldObj.focus();
                    return false;
                }
            }
function MM_openBrWindow(theURL,winName,features) { //v2.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  } 
  window.open(theURL,winName,features);
}
//-->
</SCRIPT>

Open in new window

0
Comment
Question by:weknowjack
  • 13
  • 10
23 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
yes, it's set here:

currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
                    
                    if(maxDateStr != '')
                    {
                        maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
                        //alert(maxDays);
                        if(currDays > maxDays)
                            retValue = false;
                    }

                    if(minDateStr != '')
                    {
                        minDays = parseInt(minDateStr.substr(0,2),10) + parseInt(minDateStr.substr(3,2),10)*30  + parseInt(minDateStr.substr(6,4),10)*365;
                        //alert(minDays);
                        if(currDays < minDays)
                            retValue = false;
                    }

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
currDays is the number of days since the year zero, some large number eg 13 Dec 2013 would be 1485594

minDateStr is the earliest the date can be, while maxDateStr is the maximum.  If it is for this year then they're set to something like "01/01/2013" and "31/12/2013" respectively.

minDays / maxDays are the conversion to a number so that currDays can be compared with and whether it falls between minDays and maxDays

So all you need to change is the strings: minDateStr and maxDateStr.  You have to make sure you follow the dd/mm/yyyy format or it won't calculate properly.  Note the double digits for day and month and the four digits for year.

There is an element of error in the calculation as the number of days assumes each month is 30 days and each year is 365 so you may find some dates that are in the range may say they're not close to the minimum or maximum.
0
 

Author Comment

by:weknowjack
Comment Utility
Thanks for the fast response but I'm really ignorant here.

I see the mindate and maxdate code but I don't know what to change. I want a max of 90 days between two dates and a message when it exceeds that. There would be no min since the same day is ok.

Does this code control all that?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
then you don't need the mindatestr.  You would calculate minDays based on the maxDays-90 so just combine into the condition testing if there is a maxDateStr

currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
                    
                    if(maxDateStr != '')
                    {
                        maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
                        //alert(maxDays);
                        if(currDays > maxDays)
                            retValue = false;
                    

                        minDays = maxDays - 90;
                        //alert(minDays);
                        if(currDays < minDays)
                            retValue = false;

}

Open in new window

0
 

Author Comment

by:weknowjack
Comment Utility
I copied what you have above and pasted it into the script in the page replacing what was there. It still will not validate the date selected in Jan 2014. I probably missed something but the validation seems to be functioning.

Here is the complete script:

<SCRIPT type=text/javascript>
<!--
function validateField(fieldId, fieldBoxId, fieldType, required)
            {
                fieldBox = document.getElementById(fieldBoxId);
                fieldObj = document.getElementById(fieldId);

                if(fieldType == 'text'  ||  fieldType == 'textarea'  ||  fieldType == 'password'  ||  fieldType == 'file'  ||  fieldType == 'phone'  || fieldType == 'website')
                {	
                    if(required == 1 && fieldObj.value == '')
                    {
                        fieldObj.setAttribute("class","mainFormError");
                        fieldObj.setAttribute("className","mainFormError");
                        fieldObj.focus();
                        return false;					
                    }

                }

                else if(fieldType == 'menu'  || fieldType == 'country'  || fieldType == 'state')
                {	
                    if(required == 1 && fieldObj.selectedIndex == 0)
                    {				
                        fieldObj.setAttribute("class","mainFormError");
                        fieldObj.setAttribute("className","mainFormError");
                        fieldObj.focus();
                        return false;					
                    }

                }

                else if(fieldType == 'email')
                {	
                    if((required == 1 && fieldObj.value=='')  ||  (fieldObj.value!=''  && !validate_email(fieldObj.value)))
                    {				
                        fieldObj.setAttribute("class","mainFormError");
                        fieldObj.setAttribute("className","mainFormError");
                        fieldObj.focus();
                        return false;					
                    }

                }

            }

            function validate_email(emailStr)
            {		
                apos=emailStr.indexOf("@");
                dotpos=emailStr.lastIndexOf(".");

                if (apos<1||dotpos-apos<2) 
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

            function validateDate(fieldId, fieldBoxId, fieldType, required,  minDateStr, maxDateStr)
            {
                retValue = true;

                fieldBox = document.getElementById(fieldBoxId);
                fieldObj = document.getElementById(fieldId);	
                dateStr = fieldObj.value;


                if(required == 0  && dateStr == '')
                {
                    return true;
                }


                if(dateStr.charAt(2) != '/'  || dateStr.charAt(5) != '/' || dateStr.length != 10)
                {
                    retValue = false;
                }	

                else	// format's okay; check max, min
                {
                    currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
                    //alert(currDays);

                    if(maxDateStr != '')
                    {
                        maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
                        //alert(maxDays);
                        if(currDays > maxDays)
                            retValue = false;
                    }

                    if(minDateStr != '')
                    {
                        minDays = maxDays - 90;
                        //alert(minDays);
                        if(currDays < minDays)
                            retValue = false;
                    }
                }

                if(retValue == false)
                {
                    fieldObj.setAttribute("class","mainFormError");
                    fieldObj.setAttribute("className","mainFormError");
                    fieldObj.focus();
                    return false;
                }
            }
function MM_openBrWindow(theURL,winName,features) { //v2.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  } 
  window.open(theURL,winName,features);
}
//-->
</SCRIPT>

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I'd edited my comment before you posted so I suspect you got an old copy of the code.  My thoughts were to remove the minDateStr and combine the condition code into the maxDateStr:

                   
if(maxDateStr != '')
                    {
                        maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
                        //alert(maxDays);
                        if(currDays > maxDays)
                            retValue = false;

                        minDays = maxDays - 90;
                        //alert(minDays);
                        if(currDays < minDays)
                            retValue = false;
                    }

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
That way you only set the maxDateStr and the rest calculates automatically.  Does that make sense?
0
 

Author Comment

by:weknowjack
Comment Utility
Not really cuz I tried what I understood and it won't submit now. Here is the part I changed:

            function validateDate(fieldId, fieldBoxId, fieldType, required,  minDateStr, maxDateStr)
            {
                retValue = true;

                fieldBox = document.getElementById(fieldBoxId);
                fieldObj = document.getElementById(fieldId);	
                dateStr = fieldObj.value;


                if(required == 0  && dateStr == '')
                {
                    return true;
                }


                if(dateStr.charAt(2) != '/'  || dateStr.charAt(5) != '/' || dateStr.length != 10)
                {
                    retValue = false;
                }	

                else	// format's okay; check max, min
                {
                    currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
                    //alert(currDays);

                    if(maxDateStr != '')
                    {
                        maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
                        //alert(maxDays);
                        if(currDays > maxDays)
                            retValue = false;

                        minDays = maxDays - 90;
                        //alert(minDays);
                        if(currDays < minDays)
                            retValue = false;
                    }
                                            

                if(retValue == false)
                {
                    fieldObj.setAttribute("class","mainFormError");
                    fieldObj.setAttribute("className","mainFormError");
                    fieldObj.focus();
                    return false;
                }
            }
function MM_openBrWindow(theURL,winName,features) { //v2.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  } 
  window.open(theURL,winName,features);
}

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
You're missing a closing parenthesis } after the maxDateStr != '' condition.  It needs to be:

function validateDate(fieldId, fieldBoxId, fieldType, required,  minDateStr, maxDateStr)
{
  retValue = true;
  
  fieldBox = document.getElementById(fieldBoxId);
  fieldObj = document.getElementById(fieldId);	
  dateStr = fieldObj.value;
  
  
  if(required === 0  && dateStr === '')
  {
    return true;
  }
  
  
  if(dateStr.charAt(2) != '/'  || dateStr.charAt(5) != '/' || dateStr.length != 10)
  {
    retValue = false;
  }	
  
  else	// format's okay; check max, min
  {
    currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365;
    //alert(currDays);
    
    if(maxDateStr !== '')
    {
      maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
      //alert(maxDays);
      if(currDays > maxDays)
        retValue = false;
      
      minDays = maxDays - 90;
      //alert(minDays);
      if(currDays < minDays)
        retValue = false;
    }
  }                        
  
  if(retValue === false)
  {
    fieldObj.setAttribute("class","mainFormError");
    fieldObj.setAttribute("className","mainFormError");
    fieldObj.focus();
    return false;
  }
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!=='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  } 
  window.open(theURL,winName,features);
}

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Note that I've updated the equality to use === and !== to make sure that type conversion isn't happening.
0
 

Author Comment

by:weknowjack
Comment Utility
I'm sorry. I checked it all and it still will not validate. I now have:

                else	// format's okay; check max, min
  				{
    				currDays = parseInt(dateStr.substr(0,2),10) + parseInt(dateStr.substr(3,2),10)*30  + parseInt(dateStr.substr(6,4),10)*365; //alert(currDays);
    
    				if(maxDateStr !== '')
    				{
      				maxDays = parseInt(maxDateStr.substr(0,2),10) + parseInt(maxDateStr.substr(3,2),10)*30  + parseInt(maxDateStr.substr(6,4),10)*365;
      //alert(maxDays);
      				if(currDays > maxDays)
        		retValue = false;
      	
      				minDays = maxDays - 90;      //alert(minDays);
      				if(currDays < minDays)
        			retValue = false;
    			}
  			} 

                if(retValue == false)
                {
                    fieldObj.setAttribute("class","mainFormError");
                    fieldObj.setAttribute("className","mainFormError");
                    fieldObj.focus();
                    return false;
                }
            }

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Can you show the code that is calling the validate script please.
0
 

Author Comment

by:weknowjack
Comment Utility
I guess it would be the first submit button (code below). This is a three part form collapsed/expanded to paginate the user experience.

the form

The problem occurs on the first submit on the first "page" of the form. That is the only "page" that requires validation. There is a simple security script on the last page. The entire form can be found here. If you want to test it yourself, please use "test" in name and email fields and 000-000-0000 in the phone field.
Once the first step validates there shouldn't be further messages except for not filling in the security question.
<input style="font-size: 16px;" type=button onClick="if (validatePage1()) { collapseElem('page_1'); expandElem('page_2');}" class="mainForm" value="Continue to Your Item List »"/>

Open in new window

0
 

Author Comment

by:weknowjack
Comment Utility
The validation script is in the page, not an external file.
0
 

Author Comment

by:weknowjack
Comment Utility
The calendar script we are using is found here
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
Comment Utility
Ok, the formula is based on dd/mm/yyyy not mm/dd/yyyy so you'll need to change it as follows to calculate the days:

parseInt(maxDateStr.substr(0,2),10)*30 + parseInt(maxDateStr.substr(3,2),10)  + parseInt(maxDateStr.substr(6,4),10)*365;

Open in new window


see here for a demo (type the date in mm/dd/yyyy format): http://jsbin.com/otUTUlu/1/edit?js,console,output
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I've also tried to contact the calendar control site but it's down.

The calendar control should be able to restrict to certain dates.  You can also make it so that the input textbox is unable to be typed in and the calendar icon has to be clicked.  Alternatively clicking in the box displays the calendar.
If the calendar control you're using doesn't do this then use this jquery one.  It's very good:  http://jqueryui.com/datepicker/

A note on validation... This should be done in full on the server and not the client.  To validate on the client is just a way of helping the user enter the right information but shouldn't be used to validate the form.  I would be able to bypass all your javascript validation and submit what I like but using the console so you'd want to make sure that all inputs can be handled by your server.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
http://jsbin.com/otUTUlu/2/edit?js,console,output

This shows how easy it is to restrict the jquery calendar control to allow dates 90 days in the past and 1 month and 10 days into the future, as an example.

$(function() {
    $( "#field_1" ).datepicker({ 
minDate: "-90D", 
maxDate: "+1M +10D" 
});
  });

Open in new window

0
 

Author Comment

by:weknowjack
Comment Utility
This one seems to have fixed it: ID: 39720406

I am curious what that parse line is actual specifying. It is cryptic to me. I wasn't sure where to put it, so I put it in two places... currDays and maxDays. Did I do this right?

else	// format's okay; check max, min
                {
                    currDays = parseInt(maxDateStr.substr(0,2),10)*30 + parseInt(maxDateStr.substr(3,2),10)  + parseInt(maxDateStr.substr(6,4),10)*365;
                    //alert(currDays);

                    if(maxDateStr != '')
                    {
                        maxDays = parseInt(maxDateStr.substr(0,2),10)*30 + parseInt(maxDateStr.substr(3,2),10)  + parseInt(maxDateStr.substr(6,4),10)*365;
                        //alert(maxDays);
                        if(currDays > maxDays)
                            retValue = false;
                    }

                    if(minDateStr != '')
                    {
                        minDays = parseInt(minDateStr.substr(0,2),10) + parseInt(minDateStr.substr(3,2),10)*30  + parseInt(minDateStr.substr(6,4),10)*365;
                        //alert(minDays);
                        if(currDays < minDays)
                            retValue = false;
                    }
                }

Open in new window


The values from this form drop into another piece of management software. It reads the values from the email that the form send on submit. Apparently whatever was entered before was compatible, except for the date limitation. I implemented your parse function and it worked. I don't know if I entered it correctly and I don't yet know if the management app accepted it, but I expect that it did, since I was finally directed to the confirmation page at the end.
0
 
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 500 total points
Comment Utility
Ok then we won't change anything else.  Better to keep something that's working :)

You're almost there but your currDays and minDays calculations aren't correct.  You need to replace maxDateStr with dateStr for currDays and minDateStr for minDays.  However this will make more sense when I explain it below:

maxDateStr is "12/31/2013" right? so substr is the function for getting part of that string.  You pass the start and end but the first character is 0 not 1.  So if I just want the text "31" then it's maxDate.Str.substr(3,2).  That is start at character 4 in the string and return it and the next one.  Character 4 is "3" and the next character is "1".
So you get "31" but it's a string not a number.  The parseInt function looks at a string and converts it to a number (if it is a number).  If you try "a" or another non number you just get NaN (Not a Number).

So by using the substr and the parseInt funtions we're able to extract the day, month and year from the maxDateStr string.  They are all converted to days (obviously nothing needs to be done to days) and added together.  That gives the number of days since the year 0.  It gives a benchmark to compare with.

parseInt(maxDateStr.substr(0,2),10)*30  <===== Gets the month, the first 2 digits, converts to an integer and multiplies by 30 to get the number of days in the month.  This is what I was saying before that someone has made the decision to approximate this by using 30 (and not 31 or 28/29 for Feb etc)

parseInt(maxDateStr.substr(3,2),10)  <========= Gets the day from the 4th and 5th digits

parseInt(maxDateStr.substr(6,4),10)*365 <=== Gets the year from the 7th character to the 10th and cconverts it to an integer then multiplies by 365.  Like the month this is approximated to 365 days in a year.

Adding all of these together gives you a "distance" from the year 0 in days.

So when you're comparing currDays (which is what the user selects) with your maxDateStr of 12/31/2013 you're able to see whether the date the user selects is greater or less (or equal) to the maximum date you'll allow.  By then setting the minDays to maxDays-90 then you're saying that the minimum number of days since 0 is just 90 days less than your maximum.
0
 

Author Comment

by:weknowjack
Comment Utility
You are obviously very experienced at this. Wish I was. Reading your explanation, I get the feeling there is some prior knowledge necessary to grab what you so carefully explain. I'm just an empty bucket here. I had copied what your last post included in two places. But, your comment here:

"You're almost there but your currDays and minDays calculations aren't correct.  You need to replace maxDateStr with dateStr for currDays and minDateStr for minDays.  However this will make more sense when I explain it below:"

...seems to suggest that I either did not put them in the right places or there is more to be done. The strengs vs numbers difference is Greek to me.

Sorry.

If you can be a little more elementary, I would like to understand it, but I'm not sure it is worth your time or within my IQ.

To clarify since we first began this exercise, the client (my customer) does not care how far out the date is. However, at some point he might. My comment, not his.

That said, I see the 90 days might need to be something else?

Confused. Again, sorry.
0
 
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 500 total points
Comment Utility
Please don't apologise - we're all good at different things.

The confusing thing here is, you're thinking you're setting a date but all your doing is setting a string.  The way to think of this is to put yourself in the computers shoes.  It's dumber than all of us.  It just sees "12/31/2013" as a set of characters.  May as well be "de/sjjdh/whwhw".  In other words it just thinks of it as a word not a date.

So what we do is take parts of the "word" and say interpret this as a number by using the parseInt function.

So you have all of the dates as strings.  The computer sees this as words or "strings" and can't compare the dates the way you want.  So we take the characters (day, month, year) out of the string and tell the computer to now interpret the smaller parts as a number.  Once you have a number it's easy to say if one number is bigger or smaller.

When I said before you were almost there was because you used the same date string for current date, max date, and min date.  currentDate is say today "17/12/2013", maxDate is "31/12/2013" and forget about min date for the moment.  So what we do is break apart the string.  

So lets work out currDays first.  Say "give me the first two characters" ("17").  Then say to the computer, can you see a number within the word "17"? (use parseInt).  It says yep I see the number 17.  Repeat for month (12) and year (2013).  So we end up with these three numbers: 17, 12, 2013.  Now we repeat for maxDate and get the numbers: 31, 12, 2013.  But we need them in a way the computer can compare them.  So we condense the three numbers down to one by finding the lowest common denomenator ie days.  for the first three numbers 17,12,2013 => 17 days + 12 months * 30 days in a month + 2013*365 days in a year = 735122 days.  Repeat for max days and you get: 735136.  The computer says oh that's easy now to compare those two.

Please ask more questions if you need to.
0
 

Author Closing Comment

by:weknowjack
Comment Utility
The customer does not care what date is entered. The calendar is working. We'll leave it at that. Maybe I'll learn this stuff some day. Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now