Problem converting one part of script to v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
kiwichick
Posts: 147
Joined: 21 Jan 2014, 22:03

Problem converting one part of script to v2

07 Apr 2024, 23:10

I have a script that I'm trying to convert to v2. It takes a date of birth and returns the corresponding astrological sign. I'm having a problem with the section of code that performs error checks for the entered dob. If I exclude that section of code, the script works (as long as a legitimate dob is entered). But when I include the error checking code, the script behaves as if the first error has occurred. For the life of me, I can't figure out how to fix it. Can someone please help me? To keep the script manageable, I have it in two files - the GUI is in one file, the code to split the dob and do the error checking is in the other. This also allows me to run the dob split file as a standalone script.

Code: Select all

#SingleInstance Force
#Include %A_ScriptDir%\docs
#Include ColorConstants_V2.ahk2

;TITLE FOR ALL WINDOWS
window_title := "DOBist"

;BACKGROUND IMAGES FOR INPUT AND OUTPUT WINDOWS
background_image1 := A_ScriptDir . "\images\backgrounds\background1.jpg"
background_image2 := A_ScriptDir . "\images\backgrounds\background2.jpg"

;CREATE INPUT GUI1 TO ENTER DOB
myGui := Gui()
myGui.Add("Picture", "x0 y0 w500 h500", background_image1)
myGui.SetFont("bold s14")
myGui.Add("Text", "x100 y200 w300 h25 +Center", "Please enter your date of birth")
myGui.BackColor := color_white
ogcEditdob_enter := myGui.Add("Edit", "x200 y235 w100 h30 -Multi +Center vdob_enter", "2/12/1965")
myGui.SetFont("bold s12")
ogcButtonGO := myGui.Add("Button", "x140 y275 w100 h25 +Center default", "GO")
ogcButtonGO.OnEvent("Click", lbGoButton.Bind("Normal"))
ogcButtonCLOSE := myGui.Add("Button", "x270 y275 w100 h25 +Center", "CLOSE")
ogcButtonCLOSE.OnEvent("Click", lbClose1Button.Bind("Normal"))

; GUI:
myGui.Title := window_title
myGui.Show("xCenter yCenter w500 h500")
return

lbClose1Button(A_GuiEvent, GuiCtrlObj, Info, *)
{ ; V1toV2: Added bracket
_1GuiClose:
_1GuiEscape:
	ExitApp()
} ; V1toV2: Added bracket before function
	
lbGoButton(A_GuiEvent, GuiCtrlObj, Info, *)
{ ; V1toV2: Added bracket
    oSaved := myGui.Submit()
    dob_enter := oSaved.dob_enter
	myGui.Destroy()
	#Include dobsplit_V2.ahk2

MsgBox(sign)
ExitApp()
} ; V1toV2: Added bracket in the end

Code: Select all

; dob_enter := "2/12/1965"  ; uncomment only when using as standalone

; ERROR CHECK DOB
if (dob_enter := "")
	{
	MsgBox("No date of birth was entered. Please re-enter your date of birth.")
	Reload()
	}
if (dob_enter := A_Space)
	{
	MsgBox("No date of birth was entered. Please re-enter your date of birth.")
	Reload()
	}
If (dob_enter := "dd/mm/yyyy")
	{
	MsgBox("No date of birth was entered. Please re-enter your date of birth.")
	Reload()
	}
If !InStr(dob_enter, "/")
	{
	MsgBox("An invalid date of birth was entered. Please re-enter your date of birth.")
	Reload()
	}
If (dob_enter ~= "i)(|||a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|\.||)")
	{
	MsgBox("An invalid date of birth was entered. Please re-enter your date of birth.")
	Reload()
	}

;SPLIT DOB_ENTER
dob_split := StrSplit(dob_enter,"/")

	;CHECK YEAR HAS 4 NUMBERS
	If (StrLen(dob_split[3]) != 4)
	{
	MsgBox("The year must have 4 numbers. Please re-enter your date of birth.")
	Reload()
	}

	If (dob_split[1] ~= "^(?i:1|2|3|4|5|6|7|8|9)$")
	dob_pad1 := 0 . dob_split[1] 
	else dob_pad1 := dob_split[1]

	If (dob_split[2] ~= "^(?i:1|2|3|4|5|6|7|8|9)$")
	dob_pad2 := 0 . dob_split[2] 
	else dob_pad2 := dob_split[2]

	dob_final := dob_pad2 . "" . dob_pad1

	If (dob_final >= 101 && dob_final <= 120)
		sign := "Capricorn"

	If (dob_final >= 121 && dob_final <= 218)
		sign := "Aquarius"

	If (dob_final >= 219 && dob_final <= 320)
		sign := "Pisces"

	If (dob_final >= 321 && dob_final <= 420)
		sign := "Aries"

	If (dob_final >= 421 && dob_final <= 521)
		sign := "Taurus"

	If (dob_final >= 522 && dob_final <= 621)
		sign := "Gemini"

	If (dob_final >= 622 && dob_final <= 723)
		sign := "Cancer"

	If (dob_final >= 724 && dob_final <= 823)
		sign := "Leo"

	If (dob_final >= 824 && dob_final <= 923)
		sign := "Virgo"

	If (dob_final >= 924 && dob_final <= 1023)
		sign := "Libra"

	If (dob_final >= 1024 && dob_final <= 1121)
		sign := "Scorpio"

	If (dob_final >= 1122 && dob_final <= 1221)
		sign := "Sagittarius"

	If (dob_final >= 1222 && dob_final <= 1231)
		sign := "Capricorn"
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Problem converting one part of script to v2

08 Apr 2024, 01:14

Code: Select all

if (dob_enter := "")
Is assigning the variable dob_enter to be blank.

try: if (dob_enter = "")

Same goes for the next 2 comparisons.

HTH
kiwichick
Posts: 147
Joined: 21 Jan 2014, 22:03

Re: Problem converting one part of script to v2

08 Apr 2024, 18:55

Xtra wrote:
08 Apr 2024, 01:14

Code: Select all

if (dob_enter := "")
Is assigning the variable dob_enter to be blank.

try: if (dob_enter = "")

Same goes for the next 2 comparisons.

HTH
Thank you very much for that. I had tried that and it didn't work but something else that I've subsequently fixed must have had an effect on it. I now have the first three checks working. The fourth one works as it is, but the last one doesn't. Do you have any suggestions for that one?
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Problem converting one part of script to v2

08 Apr 2024, 21:11

Code: Select all

If !(dob_enter ~= "^(\d\d\/\d\d\/\d{4})$")
	{
	MsgBox("An invalid date of birth was entered. Please re-enter your date of birth.")
	Reload()
	}
You can probably replace all the others checks with just this one.

HTH
kiwichick
Posts: 147
Joined: 21 Jan 2014, 22:03

Re: Problem converting one part of script to v2

10 Apr 2024, 01:03

Xtra wrote:
08 Apr 2024, 21:11

Code: Select all

If !(dob_enter ~= "^(\d\d\/\d\d\/\d{4})$")
	{
	MsgBox("An invalid date of birth was entered. Please re-enter your date of birth.")
	Reload()
	}
You can probably replace all the others checks with just this one.

HTH
Thanks again but, unfortunately, it will only work if the day or month contains two digits. I allow for single digit entry further down the script by padding single day or month with a leading zero but that happens after the error checks are made.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Problem converting one part of script to v2

10 Apr 2024, 02:02

You can have it handle 1 or 2 digits by changing the regex needle:

Code: Select all

If !(dob_enter ~= "^(\d\d?\/\d\d?\/\d{4})$")
kiwichick
Posts: 147
Joined: 21 Jan 2014, 22:03

Re: Problem converting one part of script to v2

10 Apr 2024, 02:27

Xtra wrote:
10 Apr 2024, 02:02
You can have it handle 1 or 2 digits by changing the regex needle:

Code: Select all

If !(dob_enter ~= "^(\d\d?\/\d\d?\/\d{4})$")
Thank you, thank you, thank you!!!!! I tried using

Code: Select all

If !(dob_enter ~= "^(\d{1}|\d{2}\/\d{1}|\d{2}\d{4})$")
but it didn't work. I'm not very good with regex and had forgotten about using ?
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Problem converting one part of script to v2

10 Apr 2024, 04:10

The way to indicate a quantity of one or two digits with the braces approach is:

Code: Select all

If !(dob_enter ~= "^(\d{1,2}\/\d{1,2}\d{4})$")
kiwichick
Posts: 147
Joined: 21 Jan 2014, 22:03

Re: Problem converting one part of script to v2

10 Apr 2024, 04:12

boiler wrote:
10 Apr 2024, 04:10
The way to indicate a quantity of one or two digits with the braces approach is \d{1,2}.
Thank you :D

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: WarlordAkamu67 and 48 guests