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

 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

LVL 18
Rajar AhmedConsultantAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
fsze88Connect With a Mentor Commented:


 
<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
 
fsze88Commented:
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
 
Rajar AhmedConsultantAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
fsze88Commented:
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
 
Rajar AhmedConsultantAuthor Commented:
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
 
Rajar AhmedConsultantAuthor Commented:
thanks :)
0
All Courses

From novice to tech pro — start learning today.