Archive: How to detect if a Text field is filled


How to detect if a Text field is filled
Hello,

I would like to enable Next button only if every text field is filled:


Function Veto1

!insertmacro MUI_HEADER_TEXT "Information" $(InfoUser)

GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 0

nsDialogs::Create 1018
Pop $Dialog

${If} $Dialog == error
Abort

${EndIf}

${NSD_CreateLabel} 0 0 100% 12u $(L_Clinique)
Pop $Label

${NSD_CreateText} 0 10u 100u 12u $C_Clinique
Pop $Text
${NSD_SetTextLimit} $Text 25
${NSD_setFocus} $Text

${NSD_CreateLabel} 0 25u 100% 12u $(L_Service)
Pop $Label3

${NSD_CreateText} 0 35u 100u 12u $C_Service
Pop $Text3
${NSD_SetTextLimit} $Text3 40

${NSD_CreateLabel} 0 50u 100% 12u $(L_NomVets)
Pop $Label4

${NSD_CreateText} 0 60u 100u 12u $C_NomVets
Pop $Text4
${NSD_SetTextLimit} $Text4 55

${NSD_CreateLabel} 0 75u 100% 12u $(L_Adresse1)
Pop $Label1

${NSD_CreateText} 0 85u 200u 12u $C_Adresse1
Pop $Text1
${NSD_SetTextLimit} $Text1 80

${NSD_CreateLabel} 0 100u 100% 12u $(L_Adresse2)
Pop $Label2

${NSD_CreateText} 0 110u 200u 12u $C_Adresse2
Pop $Text2
${NSD_SetTextLimit} $Text2 80

nsDialogs::Show


FunctionEnd

Function Veto1Leave

StrCmp $Text "" -1 0
StrCmp $Text1 "" -2 0
StrCmp $Text3 "" -3 0
StrCmp $Text4 "" -4 0
EnableWindow $0 1

${NSD_GetText} $Text $C_Clinique
${NSD_GetText} $Text1 $C_Adresse1
${NSD_GetText} $Text2 $C_Adresse2
${NSD_GetText} $Text3 $C_Service
${NSD_GetText} $Text4 $C_NomVets


FunctionEnd
Do I have to make an .INI file or am I on the good way to do it ?

First, disable the Next button before you nsDialogs::Show. Then add OnChange macros for every text field, which all refer to the same function. Make this function check the contents of all fields (gettext macro), and then depending on their contents:
${If} $Field1 != ""
${AndIf} $Field2 != ""
...
${AndIf} $FieldLast != ""
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 1
${Else}
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 0
${EndIf}

That's all.


And of course you first need to call NSD_GetText and then check its value


Yeah, that's what I said. :)


Thanks you !
It works well.
I let the new script below :


Function Veto1

!insertmacro MUI_HEADER_TEXT "Information" $(InfoUser)

GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 0

nsDialogs::Create 1018
Pop $Dialog

${If} $Dialog == error
Abort

${EndIf}

${NSD_CreateLabel} 0 0 100% 12u $(L_Clinique)
Pop $Label

${NSD_CreateText} 0 10u 100u 12u $C_Clinique
Pop $Text
${NSD_SetTextLimit} $Text 25
${NSD_setFocus} $Text
${NSD_OnChange} $Text Fd_Filled

${NSD_CreateLabel} 0 25u 100% 12u $(L_Service)
Pop $Label3

${NSD_CreateText} 0 35u 100u 12u $C_Service
Pop $Text3
${NSD_SetTextLimit} $Text3 40
${NSD_OnChange} $Text3 Fd_Filled


${NSD_CreateLabel} 0 50u 100% 12u $(L_NomVets)
Pop $Label4

${NSD_CreateText} 0 60u 100u 12u $C_NomVets
Pop $Text4
${NSD_SetTextLimit} $Text4 55
${NSD_OnChange} $Text4 Fd_Filled

${NSD_CreateLabel} 0 75u 100% 12u $(L_Adresse1)
Pop $Label1

${NSD_CreateText} 0 85u 200u 12u $C_Adresse1
Pop $Text1
${NSD_SetTextLimit} $Text1 80
${NSD_OnChange} $Text1 Fd_Filled

${NSD_CreateLabel} 0 100u 100% 12u $(L_Adresse2)
Pop $Label2

${NSD_CreateText} 0 110u 200u 12u $C_Adresse2
Pop $Text2
${NSD_SetTextLimit} $Text2 80
${NSD_OnChange} $Text2 Fd_Filled

nsDialogs::Show

FunctionEnd

Function Fd_Filled

${NSD_GetText} $Text $C_Clinique
${NSD_GetText} $Text1 $C_Adresse1
${NSD_GetText} $Text2 $C_Adresse2
${NSD_GetText} $Text3 $C_Service
${NSD_GetText} $Text4 $C_NomVets

${If} $C_Clinique == ""
${OrIf} $C_Adresse1 == ""
${OrIf} $C_Adresse2 == ""
${OrIf} $C_Service == ""
${OrIf} $C_NomVets == ""
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 0
${Else}
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 1
${EndIf}

FunctionEnd

Thank you for reply !
I let my new script below :


Function Veto1

!insertmacro MUI_HEADER_TEXT "Information" $(InfoUser)

GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 0

nsDialogs::Create 1018
Pop $Dialog

${If} $Dialog == error
Abort

${EndIf}

${NSD_CreateLabel} 0 0 100% 12u $(L_Clinique)
Pop $Label

${NSD_CreateText} 0 10u 100u 12u $C_Clinique
Pop $Text
${NSD_SetTextLimit} $Text 25
${NSD_setFocus} $Text
${NSD_OnChange} $Text Fd_Filled

${NSD_CreateLabel} 0 25u 100% 12u $(L_Service)
Pop $Label3

${NSD_CreateText} 0 35u 100u 12u $C_Service
Pop $Text3
${NSD_SetTextLimit} $Text3 40
${NSD_OnChange} $Text3 Fd_Filled


${NSD_CreateLabel} 0 50u 100% 12u $(L_NomVets)
Pop $Label4

${NSD_CreateText} 0 60u 100u 12u $C_NomVets
Pop $Text4
${NSD_SetTextLimit} $Text4 55
${NSD_OnChange} $Text4 Fd_Filled

${NSD_CreateLabel} 0 75u 100% 12u $(L_Adresse1)
Pop $Label1

${NSD_CreateText} 0 85u 200u 12u $C_Adresse1
Pop $Text1
${NSD_SetTextLimit} $Text1 80
${NSD_OnChange} $Text1 Fd_Filled

${NSD_CreateLabel} 0 100u 100% 12u $(L_Adresse2)
Pop $Label2

${NSD_CreateText} 0 110u 200u 12u $C_Adresse2
Pop $Text2
${NSD_SetTextLimit} $Text2 80
${NSD_OnChange} $Text2 Fd_Filled

nsDialogs::Show


FunctionEnd

Function Fd_Filled

${NSD_GetText} $Text $C_Clinique
${NSD_GetText} $Text1 $C_Adresse1
${NSD_GetText} $Text2 $C_Adresse2
${NSD_GetText} $Text3 $C_Service
${NSD_GetText} $Text4 $C_NomVets

${If} $C_Clinique == ""
${OrIf} $C_Adresse1 == ""
${OrIf} $C_Adresse2 == ""
${OrIf} $C_Service == ""
${OrIf} $C_NomVets == ""
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 0
${Else}
GetDlgItem $0 $HWNDPARENT 1
EnableWindow $0 1
${EndIf}

FunctionEnd

As already mentioned you need to set up OnChange events for each text box. There's no point disabling the buttons on page leave because that's too late! Personally I just throw an error message in page leave.

Stu


About OnChange events, Didn't I make ? Did I missunderstand ?
I didn't copy the page leave because I removed the StrCmp lines but it's clearer in my mind now.

Thanks Stu


Yeah, you did it correctly.

(Although, I just realized you might need to do a 'call Fd_Filled' before you nsDialogs::Show, so that the Next button will not be disabled when the user goes Back from a later page.)