Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I validate date and time in one field?

Posted on 2016-09-19
4
Medium Priority
?
79 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 2000 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

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

610 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