Solved

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

Posted on 2014-12-05
2
166 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
Comment Utility
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
Comment Utility
Worked great . Thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

744 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