regex help: HH:MM

  • Thread starter Thread starter Cirene
  • Start date Start date
C

Cirene

I want to validate whether a user has entered data correctly in a textbox.

The format should be HOUR:MINUTE. They should ALWAYS enter a value, 2
digits always for the minute.

OPTIONAL: Minutes should never go over 60.

These will pass:
45:14 (45 hrs, 14 min)
01:00 (1 hr, no min)
2:45 (2 hr, 45 min)

These will fail:
23 (no minutes)
23:1 (minutes should be 2 digits)
23:122 (too many minutes)

Thanks!!!!
 
TimeSpan has a methods TryParse which will do it.
Not sure about 23:1 though. TryParse might eat it.

You can always use DateTime.TryParseExact just prepend "1/1/200" to your
time string. You know if it fails then the reason is time portion. Cause
you have a date portion correct.

George.
 
Cirene explained on 3-6-2008 :
I want to validate whether a user has entered data correctly in a textbox.

The format should be HOUR:MINUTE. They should ALWAYS enter a value, 2 digits
always for the minute.

OPTIONAL: Minutes should never go over 60.

These will pass:
45:14 (45 hrs, 14 min)
01:00 (1 hr, no min)
2:45 (2 hr, 45 min)

These will fail:
23 (no minutes)
23:1 (minutes should be 2 digits)
23:122 (too many minutes)

Thanks!!!!

use a Regexvalidator with this ValidationExpression:

[0-9]{1,2}:[0-5][0-9]

one or two digit hour, ":", two-digit minutes with the first digit
between 0 and 5.

Hans Kesting
 
awesome thanks yall!
Hans Kesting said:
Cirene explained on 3-6-2008 :
I want to validate whether a user has entered data correctly in a
textbox.

The format should be HOUR:MINUTE. They should ALWAYS enter a value, 2
digits always for the minute.

OPTIONAL: Minutes should never go over 60.

These will pass:
45:14 (45 hrs, 14 min)
01:00 (1 hr, no min)
2:45 (2 hr, 45 min)

These will fail:
23 (no minutes)
23:1 (minutes should be 2 digits)
23:122 (too many minutes)

Thanks!!!!

use a Regexvalidator with this ValidationExpression:

[0-9]{1,2}:[0-5][0-9]

one or two digit hour, ":", two-digit minutes with the first digit between
0 and 5.

Hans Kesting
 
Back
Top