gchiropoulos
asked on
Javascript Validation - NEED HELP!
Hello all I need some serious help since I have NO IDEA about Javascript. This is the scenario. I have an asp page that has a form called addcontact. There are two input boxes on this form, checkindate and checkoutdate, that will be populated via a javascript calendar popup that is already completered, so data is always the same. mm/dd/yyyy. So far so good. I already have a javascript file called gen_validatorv2.js that is included in my asp page. This js file has a bunch of pre-done functions that I am using to validate email addresses, phone numbers and so on, but there is no function to compare dates.
Can someone help me by writing a function that I can call using the onclick property of my submit button that will do the following:
If checkindate and checkoutdate are both null then ALLOW the form to post.
If checkindate is not null and checkoutdate is null then DO NOT ALLOW the form to post and errormessage alert should be "Checkout date must be filled in"
If checkoudate is not null and checkindate is null then DO NOT ALLOW the form to post and errormessage alert should be "Checkin date must be filled in"
If checkoutdate <= checkindate then DO NOT ALLOW the form to post and errormessage should be "Checkout date must be later than checkin date"
This question is worth 500 points not because of the difficulty since it seems pretty straightforward (although I am clueless in JS) but because of the urgency.
Thanks
Can someone help me by writing a function that I can call using the onclick property of my submit button that will do the following:
If checkindate and checkoutdate are both null then ALLOW the form to post.
If checkindate is not null and checkoutdate is null then DO NOT ALLOW the form to post and errormessage alert should be "Checkout date must be filled in"
If checkoudate is not null and checkindate is null then DO NOT ALLOW the form to post and errormessage alert should be "Checkin date must be filled in"
If checkoutdate <= checkindate then DO NOT ALLOW the form to post and errormessage should be "Checkout date must be later than checkin date"
This question is worth 500 points not because of the difficulty since it seems pretty straightforward (although I am clueless in JS) but because of the urgency.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also change
if(date2 < date1)
to
if(date2 <= date1)
if u do not want the checkindate to be equal to the checkout date...
if(date2 < date1)
to
if(date2 <= date1)
if u do not want the checkindate to be equal to the checkout date...
ASKER
what about if both fields are empty. I need the form to post if they are both empty.
Thanks
Thanks
ASKER
forget it I figured the last part out.
if(document.frm.checkindat e.value.le ngth == 0 && document.frm.checkoutdate. value.leng th == 0)
{
return true
}
if(document.frm.checkindat
{
return true
}
ASKER
PS: archrajan ----- GREAT RESPONSE TIME. Funtion works great. You've earned these points.
G
G
Thanks for the points!
And Gotcha u figured it out to check if both fields r empty!
Put that in the beginning of the function!
And Gotcha u figured it out to check if both fields r empty!
Put that in the beginning of the function!
var date1 = new Date(document.frm.checkind
var date2 = new Date(document.frm.checkout
if(document.frm.checkindat
{
alert("Please enter checkin date");
return false
}
if(document.frm.checkoutda
{
alert("Please enter checkout date");
return false
}
if(date2.getTime < date1.getTime)
{
alert(checkout date needs to be greater than checkin date");
return false;
}
replace frm with the name of ur form