Solved

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

Posted on 2014-12-05
2
169 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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…

810 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