regex help: HH:MM

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!!!!
 
G

George Ter-Saakov

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.
 
H

Hans Kesting

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
 
C

Cirene

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,297
Latest member
EngineerD

Latest Threads

Top