date / time regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

date / time regex

Post by trent2800 »

I cant see why this wont work:

(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d [012]\d:[0-5]\d

I am attempting to make sure that the date/time is formatted this way:

mm/dd/yyyy hh:mm

Any help would be most appreciated.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

in php or javascript?
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

Post by trent2800 »

... (lowers head) ... vbscript...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

... (beats trent2800 with a rank kipper) ... ;-)

Sorry trent - you might find vbscript knowledge a little thin around here, I know I never did anything of consequence in it :-(

It works in javascript:

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta name="Author" content="Kieran Huggins - http://kieran.ca" />
	<link rel="stylesheet" href="" media="screen" />
	<style type="text/css">input{width: 400px;}</style>
	<script type="text/javascript">
		function validate(input){
			var rx = document.getElementById('regex');
			var dt = document.getElementById('date');			
			var op = document.getElementById('output');
			if(dt.value.match(rx.value)){
				op.value='match';
			}else{
				op.value='';
			}
		}
	</script>
</head>
<body onload="validate()">
  <input id="date" type="text" value="12/08/1979 15:36" onkeyup="validate()"/><br/>
  <input id="regex" type="text" value="(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d [012]\d:[0-5]\d" onkeyup="validate()"/><br/>
  <input id="output" type="text" style="color: green;"/>
</body>
</html>
Anyone else?
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

Post by trent2800 »

couldn't stand it one more second... I converted the entire thing over to javascript. ... Javascript, I'll never leave you again, old friend ...

P.S. Works great, thanks a lot.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

welcome home ;-)
Post Reply