Solved

How can I validate date and time in one field?

Posted on 2016-09-19
4
50 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 22

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 22

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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 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…

786 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