by SurfEd (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Thu 4th April 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Function to validate when a 'time' is entered into any control throughout your project
API Declarations
text1.text = ValidateTime(Text1.text)
'---------------------------------------------------------------------------------------------------------------------------------------------------------
' Function to validate times throughout project. Returns the current time if an error is encountered
'----------------------------------------------------------------------------------------------------------------------------------------------------------
Const cnstDate = #1/1/2000#
Dim t%
'checking on digits and seperator
Const sDigits = "1234567890.:;"
On Error GoTo TimeError
For t% = 1 To Len(sTime)
'remove invalid chars
If InStr(sDigits, Mid$(sTime, t%, 1)) = 0 Then
sTime = Left$(sTime, t% - 1) & Right$(sTime, Len(sTime) - t%)
t% = t% - 1
Else
' Change full stops and semi colons to a colon
If Mid$(sTime, t%, 1) = "." Then sTime = Left$(sTime, t% - 1) & ":" & Right$(sTime, Len(sTime) - t%)
If Mid$(sTime, t%, 1) = ";" Then Mid$(sTime, t%, 1) = ":"
End If
Next t%
If Trim(sTime) = "" Then
ValidateTime = Format(Now, "hh:mm")
Exit Function
End If
Select Case Len(sTime)
Case 0
Exit Function
Case 1
sTime = "0" & sTime & ":00"
Case 2
sTime = sTime & ":00"
Case 3
t% = InStr(sTime, ":")
If t% = 0 Then sTime = Left$(sTime, 1) & ":" & Right$(sTime, 2)
Case 4
t% = InStr(sTime, ":")
If t% = 0 Then sTime = Left$(sTime, 2) & ":" & Right$(sTime, 2)
Case 5
sTime = Left$(sTime, 2) & ":" & Right$(sTime, 2)
End Select
' Check if it is a valid time
If IsDate(cnstDate & " " & sTime) = True Then
ValidateTime = sTime
Else
ValidateTime = Format(Now, "hh:mm")
End If
Exit Function
TimeError:
ValidateTime = Format(Now, "hh:mm")
Exit Function
End Function
No comments have been posted about Function to validate when a 'time' is entered into any control throughout your project. Why not be the first to post a comment about Function to validate when a 'time' is entered into any control throughout your project.