Date Field rules using JavaScript questions

rss

Posted by Jdubs - Dec 31 ’14 at 10:21

Here's my code:

var calcDate = new Date();

var Month = calcDate.getMonth() + 1;

var Day = calcDate.getDate() + 3;

var Year = calcDate.getFullYear();

var vStartDate = Month + "/" + Day + "/" + Year;

fd.field('DueDate').value(vStartDate);

 

fd.onsubmit(function()

{

if (fd.field('DueDate').value() < vStartDate)

{

alert("Please enter a Due Date that is 3 or more days in the future.");

return false;

}

else

{

return true;

}

})

 

This was working, but recently stopped working a couple of days ago.

 

Scenario:

  1. On 12/29/2014, users opened the form and tried to submit a form for 1/4/2015 (at least 3 days later, which is correct.)
  2. The alert "Please enter a Due Date that is 3 or more days in the future" pops up (which 1/4/2015 obviously meets that criteria).

Is this because it's the NEW YEAR (2015)?  Or is it because it's the END of the MONTH? (My code above pre-populates a date of 12/32/2014, which obviously isn't possible.

 

Hope that question makes sense.  Thank you!

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 8 months 29 days
    #1 by Dmitry Kozlov Jan 2 ’15 at 06:00

    Hi,

    Yes, you prepopulated the field with an incorrect value. Use the following code to get the current date plus 3 days:

    var calcDate = new Date();
    calcDate.setDate(calcDate.getDate() + 3);
    
    var Month = calcDate.getMonth() + 1;
    var Day = calcDate.getDate();
    var Year = calcDate.getFullYear();
    var vStartDate = Month + "/" + Day + "/" + Year;
    
  • Jdubs
    Member
    Member for: 4 years 6 months 1 day
    #2 by Jdubs Jan 2 ’15 at 09:27

    [#1]: Thank you SO much!  This works perfectly!!!  I really really appreciate it.

     

    Just for my own understanding:  the reason why your code works and mine doesn't is because the "setDate" method understands that month days that are > 31 aren't possible, so it parses out the correct date for the next month? Is that correct?

    I'm guessing when I did calDate.getDate() + 3, I set the date to a literal number without any method that could parse the number and understand that a day of "32" or greater "doesn't make sense."

    Thanks so much Dmitry.  Really hoping long-term that I start to grasp JavaScript in a way that you do.  You're really good.

  • Jdubs
    Member
    Member for: 4 years 6 months 1 day
    #3 by Jdubs Jan 2 ’15 at 12:31

    [#2]: One more question.

    After testing, the code you provided works, but if a date from the previous year (2014) is selected, then the code no longer recognizes the previous year as "less than" the current year.  (For Example:  12/25/2014 is not less than 1/5/2015.

     

    I've fixed this with the following code:

     

    fd.onsubmit(function()

    {

    var x = fd.field('DueDate').value();

    var cYear = x.slice(-4);

    if (fd.field('DueDate').value() < vStartDate || cYear == 2014)

    {

    alert("Please enter a Due Date that is 3 or more days in the future.");

    return false;

    }

    else {return true;}

    });

     

    It seems more of a workaround than a true solution.  I was just wondering why the below logic wouldn't work for previous years as well:

     

    if (fd.field('DueDate').value() < vStartDate

     

    Thanks Dmitry.

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 8 months 29 days
    #4 by Dmitry Kozlov Jan 5 ’15 at 05:02

    [#3]: Hi,

    You shouldn't compare dates as strings. First, you need to transform them into Date objects:

    var dateParts = fd.field('Date').value().split('/');
    var date = new Date(dateParts[2], dateParts[0]-1, dateParts[1]);
    
  • Jdubs
    Member
    Member for: 4 years 6 months 1 day
    #5 by Jdubs Jan 20 ’15 at 05:14

    [#4]: Is there a way to compare the new Date() objects directly?

     

    Can you somehow do

    New date() + get.date() + 3

    - (substract)

    New date()?

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 5 years 8 months 29 days
    #6 by Dmitry Kozlov Jan 21 ’15 at 04:59

    [#5]: Hi,

    Yes, you can compare Date objects directly but first you need to convert field value to the Date object as I demonstrated above.

Displaying 1 to 6 of 6 messages