Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error checking-time format 1

Status
Not open for further replies.

bbvic

Technical User
Oct 21, 2004
51
US
hi..
I have a time input text box.
Well..my code basically checks if the hour is greater than 24 or 25 and if the minute is greater than 60 or 61...

BUT,
If the type like #$##$ or 1::20 instead of typing 12:12, how can it checks this error?

Thank you


 
A simple way to validate that the input is a valid time format, without knowing why it fails if it does, is to use catch, as in:
Code:
% set t 4:20
4:20
% set e [catch {clock scan $t}]
0
% set t 4::20
4::20
% set e [catch {clock scan $t}]
1
catch returns a "0" if there is no error, and "1" if there is.



_________________
Bob Rashkin
rrashkin@csc.com
 
Thank you bong.
if it doesn't assign the values such as set t 4::20,
if I just say..
set t $specialchar
set splitStartTime [split $t:]
set startTimeHour [lindex $splitStartTime 0]
scan $startTimeHour %d startTimeHour
set startTimeMin [lindex $splitStartTime 1]
scan $startTimeMin %d startTimeMin
, how can it catch any special charaters before split ":"??
 
The same way, if I take your meaning correctly:

set t $specialchar
set e [catch {clock scan $t}]
if $e {
<do your error handling>
} else {
set splitStartTime [split $t:]
set startTimeHour [lindex $splitStartTime 0]
scan $startTimeHour %d startTimeHour
set startTimeMin [lindex $splitStartTime 1]
scan $startTimeMin %d startTimeMin
}


_________________
Bob Rashkin
rrashkin@csc.com
 
I suppose it's worth noting that clock scan $t will also execute without error if $t is a valid date (instead of time). That's probably not what you want. Perhaps a safer check would use "regexp":

set pat {[0-9]{1,2}:[0-9]{2}}
now the result of regexp will be 1 if it's a valid time:
if [regexp $pat $t] {
set t $specialchar
set splitStartTime [split $t:]
set startTimeHour [lindex $splitStartTime 0]
scan $startTimeHour %d startTimeHour
set startTimeMin [lindex $splitStartTime 1]
scan $startTimeMin %d startTimeMin
} else {
<respond to invalid time string>
}


_________________
Bob Rashkin
rrashkin@csc.com
 
YES...That's what I want ...thank you..
 
I have one more question..
If I also want to add $newend on IF-statement
How can I do that??


set pat {[0-9]{1,2}:[0-9]{2}}
if [regexp $pat $newstart] {
#$newstart
set splitStartTime [split $newstart:]
set startTimeHour [lindex $splitStartTime 0]
scan $startTimeHour %d startTimeHour
set startTimeMin [lindex $splitStartTime 1]
scan $startTimeMin %d startTimeMin
#$newend
set splitEndTime [split $newend :]
set endTimeHour [lindex $splitEndTime 0]
scan $endTimeHour %d endTimeHour
set endTimeMin [lindex $splitEndTime 1]
scan $endTimeMin %d endTimeMin
}

Thank you in advance
 
got it..
if {[regexp $pat $newstart] && [regexp $pat $newend]}
 
Depending on how boneheaded your users are expected to be, you should probably protect against "abc11:30" and "1:45xyz": set pat {[red]^[/red][0-9]{1,2}:[0-9]{2}[red]$[/red]}

I'm a little green with regular expressions. Perhaps one of the more expert members can provide a more efficient pattern.

_________________
Bob Rashkin
rrashkin@csc.com
 
I tried your last comments..it works great..
thank you
 
Better have
set pat {^((0|0[0-9])|1[0-2]):[0-5][0-9]$}
or
set pat {^((0|[01][0-9])|2[0-3]):[0-5][0-9]$}

This should cover all valid time (12 hours or 24 hours ).
Just pay attention to the bounds 0:mm 12:mm 24:mm . It's up to you to decide whitch of those is a valid hour on earth


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top