?
Solved

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

Posted on 2014-12-05
2
Medium Priority
?
175 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

831 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