Date Field rules using JavaScript questions
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:
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.)
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!
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:
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.)
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
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Hi,
Yes, you prepopulated the field with an incorrect value. Use the following code to get the current date plus 3 days:
Yes, you prepopulated the field with an incorrect value. Use the following code to get the current date plus 3 days:
Code: Select all
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;
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.
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.
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.
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
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Hi,
You shouldn't compare dates as strings. First, you need to transform them into Date objects:
You shouldn't compare dates as strings. First, you need to transform them into Date objects:
Code: Select all
var dateParts = fd.field('Date').value().split('/');
var date = new Date(dateParts[2], dateParts[0]-1, dateParts[1]);
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Hi,
Yes, you can compare Date objects directly but first you need to convert field value to the Date object as I demonstrated above.
Yes, you can compare Date objects directly but first you need to convert field value to the Date object as I demonstrated above.
-
- Information
-
Who is online
Users browsing this forum: No registered users and 12 guests