Cannot validate a date as an edittext view

Mahares

Expert
Licensed User
Longtime User
The below code does not trap and validate a date entered in an edittext box if the user accidently adds some extra characters at the end of the date as shown: 2/06/2012kh. The trap works othewise if the characters are inserted inside the string. Any idea?

Dim StarD as edittext
Dim DateTick As Long
Try
DateTick = DateTime.DateParse(StartD.text)
Catch
Msgbox("The Start date you selected may not be correct or missing the slash/.", "")
Return
End Try
 

margret

Well-Known Member
Licensed User
Longtime User
B4X:
'***Setup Code
Dim mbirth As String
mbirth = "03/23/1934"

'***Calling Code
If mbirth <> "" AND ValidDate(mbirth) Then
   'Your Code
Else
   'Your Code
End If

'***Sub for date checking
Sub ValidDate(ChkDate) As Boolean
   dcf = 0
   Try
       GoodDate = DateTime.DateParse(ChkDate)
   Catch
       GoodDate = ""
      dcf = 1
   End Try
   If dcf = 0 Then
      Return True
   Else
      Return False
   End If   
End Sub
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I tried to implement your code, but the line GoodDate="" returned an error. Although it is not shown on your code I am assuming that:
Dim dcf as Int and Dim GoodDate as long
If that is the case, you cannot have GoodDate="" (program paused error)
Actually if you check my code, it has some similarities to yours. I use DateTick and you use GoodDate.
The code I have included in my post traps as long as there is no characters at the end of the date like this: 03/23/1934xu
Thanks
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I am sorry I cannot get it to work. Thank you for your efforts.
Here is the code as I have it. The validate sub is your code:
Sub ValidDate(ChkDate) As Boolean
dcf = 0
Try
GoodDate = DateTime.DateParse(ChkDate)
Catch
GoodDate = ""
dcf = 1
End Try
If dcf = 0 Then
Return True
Else
Return False
End If
End Sub

Dim MyDate as string 'in global
Dim StartD as edittext in global
Sub btnGraph_Click
MyDate=StartD.text '
If MyDate <> "" AND ValidDate(MyDate) Then
Msgbox("The Start date you selected may not be correct or missing the slash/", "")
Return
' It traps if I enter: 3/4/2012hy. But crashes if I enter: hy3/4/2012End If

'Below goes the rest of my code where it crashes because the date I entered is hy3/4/2012.
End sub
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Try removing the slashes and ask if the result is NUMERIC, if it's not, then display an error message otherwise, process the date.

B4X:
Dim Test As String
            
Test = EditText1.Text
            
Test = Test.Replace("/", "")
            
If IsNumber(Test) Then

    DateTick = DateTime.DateParse(EditText1.Text)
    Msgbox("Date = " & DateTick, "")

Else

    Msgbox("Invalid Date entered", "")

End If
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thanks NJDude. I modifed the code a little to account for a bad month like 13 or a bad day like 33. It works in every case now. Unless you can make it more efficient:

B4X:
Dim Test As String
Dim DateTick As Long
Try
   Test=StartD.text
   Test = Test.Replace("/", "")
   If IsNumber(Test) Then
       DateTick = DateTime.DateParse(StartD.Text)
   Else
       Msgbox("Invalid Date entered because of rogue text.", "")            
      Return
   End If
      Catch
   Msgbox("Month (i.e. 13), Day (i.e. 33) or year (i.e. 212) may be out of range.", "")
   Return
    End Try
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
All you need to do is
B4X:
Sub ValidDate(ChkDate) As Boolean
   DateTime.DateFormat = "dd/MM/yyyy"      'or what ever format is required
   
   Try
      DateTime.DateParse(ChkDate)
      Return True
'      Msgbox("good date","")
   Catch
      Return False
'      Msgbox("bad date","")
   End Try
End Sub
you do not need to make the DateParse = anything. Having run the code a few times there seems to be an anomaly with DateParse, if there is anything in front of the date or the date does not match the Format then it will not work but if the format is correct but the extra letter/number is at the end then it works;
"a25/03/2012" and "33/03/2012" will not work
but "25/03/2012a" and "25/03/20121" will work, it must only use the number of characters from left to right that match the Format string.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thanks Edward:
If you take a look at the code I have in my post just before your post, you will see that with the help of NJDude I got it now to work in every case regardless of what you enter. It is a different method than yours and that of Margret, but it yields the results I am seeking. You may want to try it and compare it to yours. It does not have ay limitation, at least to this point.
 
Upvote 0
Top