Solved

Compare two form fields containing begin time and end time in AM PM format in a form

Posted on 2014-12-05
2
167 Views
Last Modified: 2014-12-05
I have a simple question. I have a form called form2 that contains two fields. the Begin Time with id "s1Time1" and an End Time with id of "s1Time2". The two fields hold the formats of HH:MM AM or PM. All I need is to be able to tell in a javascript function that the Begin Time can not be greater than the End Time. I am not using any dates or seconds in my time fields.


This is what my form looks like:

<form id="form1" name="form1" method="post" action="">
  <div id="sample1" class="ui-widget-content" style="padding: .5em;">
    <p>
      <label>Start</label>
      <br/>
      <input name="s1Time1" id="s1Time1" value=""/>
      <br/>
      <label>End</label>
      <br/>
      <input name="s1Time2" id="s1Time2" value="" />
    </p>
    <p>
      <label></label>
     
    </p>
  </div>
  <input name="submitbtn" type="submit" id="submitbtn"  value="Submit" />
  </form>
0
Comment
Question by:kbsad1
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40483097
var stime = document.getElementById("s1Time1").value;
var etime = document.getElementById("s1Time2").value;

var stime = new Date("January 01, 2000 " + stime);
stime = stime.getTime();

var etime = new Date("January 01, 2000 " + etime);
etime = etime.getTime();

if (stime > etime) {
    alert("earlier")
} else {
    alert("valid time")
}

Open in new window


Didn't do too much testing but it should be fine.

You can test here
http://jsfiddle.net/qxqfj6of/1/
0
 

Author Closing Comment

by:kbsad1
ID: 40483112
Worked great . Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now