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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

validation - how to check if date format correct

Post by DanRim » 31 Jul 2022, 07:51

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.

User avatar
mikeyww
Posts: 27114
Joined: 09 Sep 2014, 18:38

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

Post by mikeyww » 31 Jul 2022, 08:01

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

User avatar
kunkel321
Posts: 1101
Joined: 30 Nov 2015, 21:19

Re: validation - how to check if date format correct

Post by kunkel321 » 31 Jul 2022, 09:26

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.
ste(phen|ve) kunkel

User avatar
mikeyww
Posts: 27114
Joined: 09 Sep 2014, 18:38

Re: validation - how to check if date format correct

Post by mikeyww » 31 Jul 2022, 09:52

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 908 times

DanRim
Posts: 153
Joined: 20 Jul 2018, 15:16

Re: validation - how to check if date format correct

Post by DanRim » 31 Jul 2022, 10:11

@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)


User avatar
mikeyww
Posts: 27114
Joined: 09 Sep 2014, 18:38

Re: validation - how to check if date format correct

Post by mikeyww » 31 Jul 2022, 10:39

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.

User avatar
kunkel321
Posts: 1101
Joined: 30 Nov 2015, 21:19

Re: validation - how to check if date format correct

Post by kunkel321 » 07 Aug 2022, 09:01

Thanks for the information, Mike!
ste(phen|ve) kunkel

Post Reply

Return to “Ask for Help (v1)”