Page 1 of 1

validation - how to check if date format correct

Posted: 31 Jul 2022, 07:51
by DanRim
Hello,

Could some one help me with date format validation?

I want to create GUI with input field where user sometimes can enter date value. Sometimes it can be 07.07.2022 or 07.08.2021 12:12:12.

How to validate input field? Because user could also enter invalid values like 123456 or letters or other symbols?

I would be thankful for any kind feedback and help or guidance.

Re: validation - how to check if date format correct  Topic is solved

Posted: 31 Jul 2022, 08:01
by mikeyww

Code: Select all

Gui, Font, s10
Gui, Add, Text,, Date:
Gui, Add, Edit, ym vdate
Gui, Add, Button, Default, OK
Show:
Gui, Show,, Date
Return

ButtonOK:
Gui, Submit
part := StrSplit(date, [".", "-", "/"]), (part.3 < 1000) && part.3 += 2000
yyyymmdd := Format("{}{:02}{:02}", part.3, part.1, part.2)
; yyyymmdd := Format("{}{:02}{:02}", part.3, part.2, part.1)
If yyyymmdd is not date
{ MsgBox, 48, Error, Invalid date.`n`n%date%
  Gosub, Show
  Return
}
MsgBox, 64, Date, %date%
Return

Re: validation - how to check if date format correct

Posted: 31 Jul 2022, 09:26
by kunkel321
Mike (or other), I tried this code, and it does work. But I'm trying to wrap my head around how it works...

It looks like the StrSplit() is breaking the date sting into components which go into an array. But what does this (part.3 < 1000) && part.3 do? Does that detect whether the year is "yy" vs "yyyy"?

Also, how does this line work If yyyymmdd is not date ?? I Googled "ahk If is not date" and that doesn't appear to be a built-in AHK command.

Re: validation - how to check if date format correct

Posted: 31 Jul 2022, 09:52
by mikeyww
Yep. If part.3 is less than 1000, then 2000 is added to it. This generally normalizes to a yyyy format for the year. If part.3 is not less than 1000, then the expression following && is skipped.

Explained: Short-circuit boolean evaluation

I usually check the documentation before Google. https://www.autohotkey.com/docs/commands/IfIs.htm#time

image220731-1059-001_cr.png
AutoHotkey documentation
image220731-1059-001_cr.png (95.57 KiB) Viewed 971 times

Re: validation - how to check if date format correct

Posted: 31 Jul 2022, 10:11
by DanRim
@mikeyww thank you. Your script is working ^^.
I have a question is it possible to remake and use with ordinary IF statement? Because sometimes user will input date with time, so I need to validate 2 options in my case :)

I tried to write new variable, but it does not work...:

Code: Select all

secondpart:= StrSplit(date, [".", "-", "/"]), (secondpart.3 < 1000) && secondpart.3 += 2000
yyyymmddHHminsec := Format("{}{:02}{:02}{' '}{}{:02}{:02}", secondpart.3, secondpart.1, secondpart.2, secondpart.4, secondpart.5, secondpart.6, secondpart.7)


Re: validation - how to check if date format correct

Posted: 31 Jul 2022, 10:39
by mikeyww
We have entered the, "Yes, but what if..." portion of the matching exercise. To shorten the exercise, you can provide, below, a list of examples of all relevant string formats that you wish to validate.

Re: validation - how to check if date format correct

Posted: 07 Aug 2022, 09:01
by kunkel321
Thanks for the information, Mike!