View Full Version : Need help with a Regular Expression - time-check

10-23-2003, 10:23 AM
I've started looking into regExp's but it's a bit messy in my head atm, I could sure use some help!

I want to validate the value of a textbox, with a maxlength of 5.
The value in the textbox should be a time in this format: 14:33,
meaning 9:45 would be incorrect but 09:45 will be correct etc.

So... the first 2 values of the textbox has to be numeric, >= 00 and <= 24, the 3rd character has to be a :[colon] and the next 2 following characters has to be numeric, >= 00 and <= 59.

Sure I could do this with substring's, but I'm [slowly] learning regExp's and I'm thinking it'd more efficient, but I don't really know where to start :confused:

thanks in advance,

10-23-2003, 11:11 AM
For a 5-byte 24-hour time regular expression, how about:


Also, here's an online regular expression tester:


10-23-2003, 11:25 AM
Yeah, thats it.

Thanks a lot!

10-23-2003, 12:44 PM
Don't forget the ^ and the $

var pattern = /^([01][0-9]|2[0-3]):[0-5][0-9]$/;

That will restrict it to only those five characters -- without them it would be any 5 in the string.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum