Complex validating of InstallOptions user input

From NSIS Wiki
Jump to navigationJump to search
Author: KiCHiK (talk, contrib)


NOTE: If you are using 2.0b4 and above, you should use the new leave function for custom pages. This way you can validate the input without destroying the InstallOptions dialog first. See test.nsi in the Examples\InstallOptions folder for an example.

Description

This script demonstrates how to verify the user input from InstallOptions. It asks the user for a user name and a password and gives the user 3 chances to get it right. There are 3 possible user name and password matches, user1 ~ user1 password, user2 ~ user2 password, and user3 ~ user3 password.

This script requires up.ini. The contents of this file you can find below the script.

The Script

;InstallOptions Test Script
!define TEMP1 $R0 ;Temp variable
 
;The name of the installer
Name "InstallOptions Test"
 
;The file to write
OutFile "up.exe"
 
;The default installation directory
InstallDir "$PROGRAMFILES\UP"
 
;Settings
ShowInstDetails show
 
;Texts on the dialogs
DirText "Choose a directory"
LicenseText "A license"
LicenseData "..\..\License.txt"
ComponentText "Choose components"
 
;Order of pages
Page license
Page custom SetCustom "" ": User name and password"
Page components
Page directory
Page instfiles
 
Section "Read INI file"
 
  ;Get Install Options dialog user input
 
  ReadINIStr ${TEMP1} "$PLUGINSDIR\up.ini" "Field 1" "State"
  DetailPrint "User name: ${TEMP1}"
  ReadINIStr ${TEMP1} "$PLUGINSDIR\up.ini" "Field 2" "State"
  DetailPrint "Password: ${TEMP1}"
 
SectionEnd
 
Function .onInit
 
  StrCpy $2 0
 
  InitPluginsDir
  File /oname=$PLUGINSDIR\up.ini "up.ini"
 
FunctionEnd
 
Function SetCustom
 
  ;Display the InstallOptions dialog
 
  Push ${TEMP1}
 
    again: InstallOptions::dialog "$PLUGINSDIR\up.ini"
    Pop ${TEMP1}
 
  StrCmp ${TEMP1} "success" 0 continue
  ReadINIStr $0 "$PLUGINSDIR\up.ini" "Field 1" "State"
  ReadINIStr $1 "$PLUGINSDIR\up.ini" "Field 2" "State"
 
  StrCmp $0 "user1" 0 +2
    StrCmp $1 "user1 password" continue wrong
  StrCmp $0 "user2" 0 +2
    StrCmp $1 "user2 password" continue wrong
  StrCmp $0 "user3" 0 +2
    StrCmp $1 "user3 password" continue wrong
 
  wrong:
    IntOp $2 $2 + 1
	StrCmp $2 3 kill
    MessageBox MB_OK|MB_ICONSTOP "Wrong user name and password, try again"
    Goto again
 
  continue: Pop ${TEMP1}
  Return
 
  kill: MessageBox MB_OK|MB_ICONSTOP "3 times wrong, bye!"
  Quit
 
FunctionEnd

up.ini

[Settings]
NumFields=4
 
[Field 1]
Type=text
Text=asd
Left=50
Right=-1
Top=17
Bottom=29
 
[Field 2]
Type=password
Text=asd
Left=50
Right=-1
Top=33
Bottom=45
 
[Field 3]
Type=label
Text=Username:
Left=0
Right=48
Top=17
Bottom=29
 
[Field 4]
Type=label
Text=Password:
Left=0
Right=48
Top=33
Bottom=45