Solved

How can I validate date and time in one field?

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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)
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…

691 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