Solved

How can I validate date and time in one field?

Posted on 2016-09-19
4
36 Views
Last Modified: 2016-09-23
I have a text box with a date/time picker, but the box is not read only. I need to use jquery or javascript to validate the date/time entered meets the following format: 09/19/2016 15:31

That is.. I need to validate input in the text box as mm/dd/yyyy [space] 24:hour time clock. Does anyone have experience with how I can do this? If so, do you have a code example? I'm thinking maybe a regular expression? Thank you.
0
Comment
Question by:earwig75
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41805960
The following requires a two-digit hour and minute and will work for any date in the 21st century (2000-2099). It accepts both 00:00 and 24:00 as midnight.
var testdate = '09/19/2016 15:31',
    patt = /(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/20\d\d (([01]\d|2[0-3]):[0-5]\d|24:00)/;
if (patt.test(testdate) ) {
    alert('Date format is correct!');
} else {
    alert('Incorrect date format. Please provide your date and time in mm/dd/20yy hh:mm 24-hour format.');
}

Open in new window

It accepts 01 thru 31 for a day of the month regardless of what month it is. If you want to consider the month, it will get a bit more elaborate.
0
 

Author Comment

by:earwig75
ID: 41806736
Please forgive my ignorance, but why is there a comma after your first line? I need to set the date equal to the value of a field. Below is what I am using, but it fails. Can you help?

var testdate = document.forms["myForm"]["dateTimeField"].value,
    patt = /(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/20\d\d (([01]\d|2[0-3]):[0-5]\d|24:00)/;
if (patt.test(testdate) ) {
    alert('Date format is correct!');
} else {
    alert('Incorrect date format. Please provide your date and time in mm/dd/20yy hh:mm 24-hour format.');
}

Open in new window

0
 

Author Comment

by:earwig75
ID: 41806778
Nevermind, I just needed the regular expression. I got it to work by using the below, thank you very much!
$('#test').submit(function(){
    var val = $('#myBox').val();
    if(/(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/20\d\d (([01]\d|2[0-3]):[0-5]\d|24:00)/.test(val) == false) {
      alert("test");  
        return false;
    }
}
);

Open in new window

0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41806863
The comma was to separate the two variables I was declaring, testdate and patt.

You are welcome.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

707 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

13 Experts available now in Live!

Get 1:1 Help Now