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

x
?
Solved

JavaScript: Validate Correct Date/Time Format

Posted on 2011-09-08
7
Medium Priority
?
377 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 400 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 1200 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

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