Solved

JavaScript: Validate Correct Date/Time Format

Posted on 2011-09-08
7
358 Views
Last Modified: 2012-05-12
Using JavaScript, I want to test if a date and time is in the allowed format.

These values are valid:
2011-9-9 22:3:00
2001-11-11 0:23:55
2091-1-1 2:00:11


These values are NOT valid:
11-9-9 22:3:00
2001-11-11
2091-1-1 2:00:11 xyz

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>

<input type="text" id="timeDate" />
<input type="button" value="test" onclick="checkTimeDateFormat(document.getElementById('timeDate').value)" />

<script type="text/javascript">
/*<![CDATA[*/

function checkTimeDateFormat(str) {
 alert('The Format of the Date/Time field is correct.');
 alert('The Format of the Date/Time field is NOT correct.');
}

/*]]>*/
</script>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[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
  • 4
  • 2
7 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 100 total points
ID: 36504231
pattern can be match by

var str = "2001-11-11 0:23:55";
var re = new RegExp("\d{4}\-\d{1,2}\-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}", "g");
var myArray = str.match(re);

if ( myArray != null)
{
   alert("correct date");
}
0
 
LVL 7

Accepted Solution

by:
Gewgala earned 300 total points
ID: 36504266
function checkTimeDateFormat(str) {
     var patt1 = "^\\d{4,4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{2,2}$";
     var reg = new RegExp(patt1);
     if (reg.test(str))
         alert('The Format of the Date/Time field is correct.');
     else
         alert('The Format of the Date/Time field is NOT correct.');
}

Open in new window

0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 100 total points
ID: 36504875
Don't forget that a user supplied "date" can match the pattern, and still be invalid.

For example:
  \d{4} simply verifies that the string contains exactly 4 digits, so everything from
            0000 to 9999 is "valid".

It is a good idea to convert the user specified input to a Date object (see http://www.w3schools.com/js/js_obj_date.asp for more details).

Given a Date object, you can format it (e.g., using information from this article http://www.experts-exchange.com/A_484.html) to a string, and then compare the user input string with the formatted date string.

Since a Date Object constructor will, by definition, result in a valid date, user input along the lines of:

2011-02-31 12:34:56

will be converted to

2011-03-03 12:34:56

and they will not match.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 7

Expert Comment

by:Gewgala
ID: 36504923
Just for the record, the solution I posted does utilize the regex pattern of \d{4,4}, which means that the minimum amount of digits is 4 and the maximum is 4, so it has to exactly be 4.
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36505012
Forgot to add in my above comment that I am talking about the year input.  But going off what hankknight posted of valid and invalid dates, my pattern is explained as follows:

^\\d{4,4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{2,2}$

Open in new window


\\d{4,4} means 4 (and only 4) digits

\\d{1,2} means 1 or 2 digits

\\d{2,2} means 2 (and only 2) digits

My original solution works great tested with the valid/invalid date time entries hankknight supplied, in that one simple JavaScript function.

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 36505036
Thanks for the assist, and the points.

Good luck & have a great day.
0
 
LVL 7

Expert Comment

by:Gewgala
ID: 36505048
Glad I could help ;)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

717 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