0

I have two text field on which when I take my mouse,then calender along with text to enter time appears in both the textfield. First textfield should be less than another textfield.For this I used javascript but its not working.My code for this is:-

<form method="post" action="Compvac8.jsp">
Select Reference:
<select name="ref_logtime" >
<option value="Select">Select</option>
<c:forEach var="aff" items="${obj.connect()}">
<option value="${aff.value}">${aff.key} ${aff.value}</option>
</c:forEach>
</select>

<br><br>
<b>Select Date to be compared</b><br>
<p>Date: <input type="text" name="datepicker" id="datepicker"></p>

<input id="startdate" type="text" size="25" name="startdate" onclick="javascript:NewCal('startdate','ddmmmyyyy',true,24)" >
<a>
<img src="images/cal.gif" width="10" height="10" border="0" alt="Pick a date">
</a>

<input id="enddate" type="text" size="25" name="enddate" onclick="javascript:NewCal('enddate','ddmmmyyyy',true,24)" >
<a>
<img src="images/cal.gif" width="10" height="10" border="0" alt="Pick a date">
</a>

 <input type="submit"  onclick="date_comapare()" value="Submit"><br>

</form>

Javascript code is-

  function date_comapare()
{

   var d1=document.getElementById("startdate").value; // start date
   var d2=document.getElementById("enddate").value; //end date
   //if (new Date(d2).valueOf() < new Date(d1).valueOf()) 
       alert(d1);
   alert(d2);
   if(new Date(d2).getTime() < new Date(d1).getTime())

   {
      alert("Endate date should be greater than start date");  // handle  your error validation here
      return false;
   }return true;
}
</script>

I even also tried folowing in javascript-

if(d2 < d1)
   {
      alert("Endate date should be greater than start date"); 
      return false;
   }

I want my startdate to be lesser than enddate.How to do that.I can use server side validation also.

1
  • dear @tiddi rastogi, plz change getElementIdBy in your code to getElementById Commented Mar 9, 2015 at 8:38

5 Answers 5

2

Wrong function on Document, needs to be:

   var d1=document.getElementById("startdate").value; // start date
   var d2=document.getElementById("enddate").value; //end date

You're passing strings instead of variables.

if(new Date(d2).valueOf() < new Date(d1).valueOf())

Please make sure the values you're initializing Date with are valid constructor arguments. MDN has a great page here

Sign up to request clarification or add additional context in comments.

3 Comments

I tried but ,when I click submit and enddate is smaller than startdate then no error or alert message is shown.
can't I directly use d2 instead of new Date(d2)
This really depends on the value of "d2". Please refer to the page I linked.
1

You have 2 problems, it should be getElementById not getElementIdBy then d1 and d2 are variables not string literals

function date_comapare() {

    var d1 = document.getElementById("startdate").value; // start date
    var d2 = document.getElementById("enddate").value; //end date

    if (new Date(d2) < new Date(d1)) {
        alert("Endate date should be greater than start date"); // handle  your error validation here
        return false;
    }
    return true;
}

1 Comment

I tried but ,when I click submit and enddate is smaller than startdate then no error or alert message is shown.
0

You have to return true in date_compare function

<script type="text/javascript">
function date_comapare()
{

   var d1=document.getElementById("startdate").value; // start date
   var d2=document.getElementById("enddate").value; //end date

   if(new Date('d2').valueOf() < new Date('d1').valueOf())
   {
      alert("Endate date should be greater than start date");  // handle      your error validation here
      return false;
   }

   return true; // <------ here, you should return true.

}

2 Comments

why did you use single quotes inside Date() function like Date('d2') ??
I tried but ,when I click submit and enddate is smaller than startdate then no error or alert message is shown.
0

Use the date.getTime Method

if (new Date(d2).getTime()  <  new Date(d1).getTime()) {}

4 Comments

Comments are not for extended discussion; this conversation has been moved to chat.
@tiddirastogi is it the date picker is working perfectly ?? i mean when you click on the textbox then a calender will appear and there you can select your date ?? is it came like that ???
try with button type="button" and come to chat in the above comment lin
@ArunprasanthKV,it shows the alert message of if ,but when I click on ok of message box,it is being redirected to next page.
0

Well,I don't know what's the use of NewCal,If that makes a well formatted Date try this:

new Date(d2).valueOf() < new Date(d1).valueOf()

Date accept a time value as parameter that may be a number or like '2010/2/1' or 'December 17, 1995' etc.In your case,try to validate it like console.log(new Date(d1)).

To use console.log(),You have to make firebug installed in your firefox.Without that you can use alert.Put it in your function right after d1 is assigned. like var d1=...;alert(new Date(d1)) . @Arunprasanth KV '9-10-2015' is not a valid Date() parameter.

3 Comments

what does this console.log() owrkand how to use it to validate .?
What does console.log()do and how to use uit to compare values of both the input fields?
i had update fiddle with valid date time thanks for corrections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.