Solved

How to add day using Javascript and  do validation accordingly...

Posted on 2009-04-01
6
401 Views
Last Modified: 2012-05-06
 Am having a form like this , need to add the days to the strtdate using that it should find the end date , Then
using this enddate need to validate those pickdate value by making sure that any of the pickdate1,pickdate2,pickdate3
should  not exceeds the enddate .... tried get the enddate like this but it dint worked...

function validate()
                 {
                 var days = document.getElementById("days").value;
var startDate = new Date(document.getElementById("strtdate").value);
var endDate1 = new Date(startDate.getTime()+days);
alert(endDate1);
return;
}
 
 <form id="form1" runat="server">
    <div>   
   days<input  id="days" type="text" maxlength="3"  runat="server" /><br />
    strtdate<input type="text" name="strtdate" id="strtdate"   runat="server" /><br />
  <input id="enddate"  type="hidden" />
 
  pickdate1<input   runat="server" id="date1" /><br />
    pickdate2<input   runat="server" id="date2" /><br />
    pickdate3<input   runat="server" id="date3" /><br />
   
        <input id="Button1" type="button" onclick="return validate();" value="button" /><br />   
    </div>
    </form>

Open in new window

0
Comment
Question by:Rajar Ahmed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 15

Expert Comment

by:fsze88
ID: 24046671
like this?
function validate(){
var days = document.getElementById("days").value;
var startDate = new Date(document.getElementById("strtdate").value);
var pickdate1 = new Date(document.getElementById("date1").value);
var pickdate2 = new Date(document.getElementById("date2").value);
var pickdate3 = new Date(document.getElementById("date3").value);
var endDate1 = new Date(startDate.setDate(startDate.getDate()  + days ) );
 
if (endDate1 > pickdate1){
return false;
}else if (endDate1 > pickdate2){
return false;
}else if (endDate1 > pickdate3){
return false;
}
return true;
}
 
<form id="form1" runat="server">
<div>   
days<input  id="days" type="text" maxlength="3"  runat="server" /><br />
strtdate<input type="text" name="strtdate" id="strtdate"   runat="server" /><br />
<input id="enddate"  type="hidden" />
 
pickdate1<input   runat="server" id="date1" /><br />
pickdate2<input   runat="server" id="date2" /><br />
pickdate3<input   runat="server" id="date3" /><br />
 
<input id="Button1" type="button" onclick="return validate();" value="button" /><br />   
</div>
</form>

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24046800
My data entered
 days 213
strtdate 24/3/2009
pickdate1   24/5/2009
pickdate2    24/6/2009
pickdate32   4/9/2009


I GET THE alerts as, for,
alert(endDate1);
 TUE SEP 17 00:00:00 UTC+0530  2019

EVEN THOUGH END DATE is less i get all exceed messages..
 


function validate(){
var days = document.getElementById("days").value;
var startDate = new Date(document.getElementById("strtdate").value);
var pickdate1 = new Date(document.getElementById("date1").value);
var pickdate2 = new Date(document.getElementById("date2").value);
var pickdate3 = new Date(document.getElementById("date3").value);
var endDate1 = new Date(startDate.setDate(startDate.getDate()  + days ) );
alert(endDate1);
if (endDate1 > pickdate1){
alert("exceeds1");
 
}
if (endDate1 > pickdate2){
alert("exceeds2");
 
}
if (endDate1 > pickdate3){
alert("exceeds3");
 
}
return true;
}

Open in new window

0
 
LVL 15

Expert Comment

by:fsze88
ID: 24047613
try this,

<script type="text/javascript">
var strDate = "24/3/2009";
var dateArray = strDate.split("/");

var  startDate = new Date(dateArray[2],dateArray[1]-1,dateArray[0]);
var endDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + 213);

document.write(startDate + "<br/>");
document.write(endDate + "<br/>");
document.write("startDate > endDate? : " + (startDate > endDate ) + "<br/>");
document.write("endDate > startDate? : " + ( endDate> startDate ) + "<br/>");

</script>
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24048093
hi again,

 i get the proper result with your code before, But  After , modifiedyour code , to satisfy my crieteria it gets some improper values asoutput ,

My fields data
days 213
strtdate 24/3/2009
picdate1 25/3/2009

picdate23/5/2009

picdate34/6/2009

The output WAs like this.......
Tue Mar 24 00:00:00 UTC+0530 2009
Sat Jun 15 00:00:00 UTC+0530 2075
endDate > pickdate1? : true


<script type="text/javascript">
    function validate()
    {
   
    var days = document.getElementById("days").value;
   
//var strDate = "24/3/2009";
var strDate  = new Date();
strDate =document.getElementById("strtdate").value;
var dateArray = strDate.split("/");
var pickdate1 = new Date(document.getElementById("date1").value);
var pickdate2 = new Date(document.getElementById("date2").value);
var pickdate3 = new Date(document.getElementById("date3").value);
 
var  startDate = new Date(dateArray[2],dateArray[1]-1,dateArray[0]);
var endDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + days);
 
document.write(startDate + "<br/>");
document.write(endDate + "<br/>");
//document.write("startDate > endDate? : " + (startDate > endDate ) + "<br/>");
//document.write("endDate > startDate? : " + ( endDate> startDate ) + "<br/>");
 
if (endDate > pickdate1){
document.write("endDate > pickdate1? : " + ( endDate> pickdate1 ) + "<br/>");
}else if (endDate > pickdate2){
document.write("endDate > pickdate2? : " + ( endDate> pickdate2 ) + "<br/>");
}else if (endDate > pickdate3){
document.write("endDate > pickdate3? : " + ( endDate> pickdate3 ) + "<br/>");
}
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
   days<input style="WIDTH: 46px" id="days" class="reg_txt" type="text" maxlength="3" name="course_duration" runat="server" /><br />
    strtdate<input type="text" name="strtdate" id="strtdate"   runat="server" /><br />
        <input id="enddate"  type="hidden" />
    pickdate1<input name="date1"  runat="server" id="date1" /><br />
    pickdate2<input name="date1"  runat="server" id="date2" /><br />
    pickdate3<input name="date1"  runat="server" id="date3" /><br />   
        <input id="Button1" type="button" onclick="return validate();" value="button" /><br />   
   
    </div>
    </form>

Open in new window

0
 
LVL 15

Accepted Solution

by:
fsze88 earned 500 total points
ID: 24048416


 
<form >
days<input id="days" type="text" name="courseduration"/>
strtdate<input id="playme" type="text" name="strtdate" />
 
    pickdate1<input name="pickdate1"  runat="server" id="date1" /><br />
    pickdate2<input name="pickdate2"  runat="server" id="date2" /><br />
    pickdate3<input name="pickdate3"  runat="server" id="date3" /><br />   
        <input id="Button1" type="button" onclick="return validate(this.form);" value="button" /><br />   
   
</form>
 
 
 
<script type="text/javascript">
function validate(formObj){
   
  var days = parseInt(formObj.courseduration.value);
  document.write("days : " + days);
 
  strDate = formObj.strtdate.value ;
  var dateArray = strDate.split("/");
  
  var startDate = new Date(dateArray[2],dateArray[1]-1,dateArray[0]);
  var endDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + days);
 
  document.write('startDate : ' + startDate + "<br/>");
  document.write('endDate : ' + endDate + "<br/>");
  
  var strPickDate1 = formObj.pickdate1.value;
  var strPickDate2 = formObj.pickdate2.value;
  var strPickDate3 = formObj.pickdate3.value;
  var pickDate1 = new Date(strPickDate1.split("/")[2], strPickDate1.split("/")[1]-1, strPickDate1.split("/")[0]);
  var pickDate2 = new Date(strPickDate2.split("/")[2], strPickDate2.split("/")[1]-1, strPickDate2.split("/")[0]);
  var pickDate3 = new Date(strPickDate3.split("/")[2], strPickDate3.split("/")[1]-1, strPickDate3.split("/")[0]);
  document.write('pickDate1 : ' + pickDate1 + "<br/>");
  document.write('pickDate2 : ' + pickDate2 + "<br/>");
  document.write('pickDate3 : ' + pickDate3 + "<br/>");
 
    document.write("endDate > pickDate1? : " + ( endDate> pickDate1 ) + "<br/>");
    document.write("endDate < pickDate1? : " + ( endDate< pickDate1 ) + "<br/>");
    document.write("endDate > pickDate2? : " + ( endDate> pickDate2 ) + "<br/>");
    document.write("endDate > pickDate3? : " + ( endDate> pickDate3 ) + "<br/>");
 
}
 
</script>

Open in new window

0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
ID: 31565658
thanks :)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question