[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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
0
gchiropoulos
Asked:
gchiropoulos
  • 4
  • 3
1 Solution
 
archrajanCommented:

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
0
 
archrajanCommented:
<script>
function check()
{
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 < date1)
{
alert("checkout date needs to be greater than checkin date");
return false;
}
}
</script>
</BODY>
<form name = "frm">
<input type ="text" name = "checkindate">
<input type = "text" name = "checkoutdate">
<input type = "submit" onclick = "return check();">
</form>
0
 
archrajanCommented:
Also change
if(date2 < date1)
to
if(date2 <= date1)

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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Thanks
0
 
gchiropoulosAuthor Commented:
forget it I figured the last part out.

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

G
0
 
archrajanCommented:
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!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now