Link to home
Start Free TrialLog in
Avatar of gchiropoulos
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
Avatar of archrajan
archrajan


var date1 = new Date(document.frm.checkindate.value)
var date2 = new Date(document.frm.checkoutdate.value)

if(document.frm.checkindate.value.length < 10)
{
alert("Please enter checkin date");
return false
}
if(document.frm.checkoutdate.value.length < 10)
{
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
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also change
if(date2 < date1)
to
if(date2 <= date1)

if u do not want the checkindate to be equal to the checkout date...

Avatar of gchiropoulos

ASKER

what about if both fields are empty. I need the form to post if they are both empty.

Thanks
forget it I figured the last part out.

if(document.frm.checkindate.value.length == 0 && document.frm.checkoutdate.value.length == 0)
{
return true
}
PS: archrajan ----- GREAT RESPONSE TIME. Funtion works great. You've earned these points.

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!