Date Field rules using JavaScript questions
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:
- 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 KozlovAdminMember for: 9 years 8 months 16 days
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;
-
JdubsMemberMember for: 8 years 5 months 19 days
[#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.
-
JdubsMemberMember for: 8 years 5 months 19 days
[#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 KozlovAdminMember for: 9 years 8 months 16 days
[#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]);
-
JdubsMemberMember for: 8 years 5 months 19 days
[#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 KozlovAdminMember for: 9 years 8 months 16 days
[#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.
-
peter0900MemberMember for: 6 months 29 days
It is crucial to validate user-provided data before processing it through a form. Several date and time formats can be validated using regular expressions and JavaScript.
-
xfinityauthorizeMemberMember for: 3 months 15 days
The cards mentioned above are available for different shopping requirements. It is possible to give an exclusive gift card to restaurants to your loved ones to allow them to dine at the restaurant they love, such as Dominos, or even subway.
walmart gift card balance | target gift card balance | home depot gift card balance | macy's gift card balance | target.com/check balance | target.com/checkbalance | paypal login | ledger.com/start | venmo login | Walmart gift card balance | amazon.com/redeem | vanilla gift card balance | vanilla gift card balance | vanilla gift card balance | vanilla gift card balance check | vanilla gift card balance | amazon.com/code | ledger.com/start | dunkin donuts gift card balance | amazon.com/redeem | Amazon.com/code | amazon.ca/redeem | amazon.co.uk/redeem | target.com/checkbalance | target gift card balance | Sephora gift card balance | target gift card balance | target.com/check balance | eBay Gift Card Balance | Target red card login | itunes gift card balance | ebay gift card balance | fandango gift card balance | best buy gift card balance | Nike gift card balance