Solved

JavaScript: Validate Correct Date/Time Format

Posted on 2011-09-08
7
344 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Allow space in this pattern 2 46
iframe detection of parent window scale 20 59
form button worked now it doesnt anymore 9 39
How can I echo a PHP variable in AJAX function? 7 28
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now