Skip to content
⌘ NSIS Forum Archive

Number check via IntCmp

6 posts

PascalB#

Number check via IntCmp

Hi,

I have a CustomDialog with a text control (created via NSD_CreateText). During installation the user has to enter a valid number. The setup has to make sure it is valid.

Rules are:
3 digits long
only numbers, no characters
beginning with 001
ending with 999

So far i use this code for evaluation. It is the callback function for the PageLeave-Event:

Function nsDialogsAZBENPageLeave
ShowWindow $AZBENLabel ${SW_HIDE}

; Check empty string
${If} $AZBENDialogFolder == ""
Abort
${EndIf}

; Check 3 digits
StrLen $0 $AZBENDialogFolder
${If} $0 <> 3
Abort
${EndIf}

; Check numerical
; Bug - all kinds of values are considered valid: abc|12w|1q1|...
IntCmp 1$AZBENDialogFolder 1999 is999 lessthan999 morethan999
is999:
Goto done
lessthan999:
Goto done
morethan999:
Abort
done:

IntCmp 1$AZBENDialogFolder 1000 is0 lessthan0 morethan0
is0:
Abort
lessthan0:
;It cannot be < 1000 !
Goto done2
morethan0:
; Should always be > 1000 !
Goto done2
done2:
; Division by 1 to produce an error if not a number
; Bug - all kinds of values are considered valid: abc|12w|1q1|...
IntOp $0 $AZBENDialogFolder / 1
IfErrors 0 +2
Abort

${NSD_GetText} $AZBENLabel $AZBENLabel_State
ShowWindow $AZBENLabel ${SW_HIDE}
FunctionEnd
I had to tweak the code and put a 1 in front of the given value, otherwise 008 and 009 would be considered as something else by IntCmp (probably some hex 0x08 value).

Can anyone shed light on this behaviour? Is there a better function than IntCmp, like IsNumeric?

Thanks in advance!
KR, Pascal
Anders#
0 prefix is probably octal. Remove leading zeros when testing. You should probably use ${If} instead of labels.

Or simply string compare with the result of IntFmt $output "%.4u" 1$input with your original number.
Anders#
Var /Global Input
!include LogicLib.nsh
StrCpy $Input 091

${If} 1$Input U< 1001
${OrIf} 1$Input U> 1999
Abort "Invalid"
${EndIf}
or
Var /Global Input
!include LogicLib.nsh
StrCpy $Input 042

IntFmt $0 "%.4u" 1$Input
${If} $0 != 1$Input
Abort "Invalid"
${EndIf}
PascalB#
Thank you, Anders!
The second solution works pretty well.

I tried to understand the format string you provided. In the official documentation (NSIS User Manual & https://nsis.sourceforge.io/Reference/IntFmt) there are only 2 expamles. Not very helpful.

Please consider if you want to add a link to


because my research found a post here (from 2002)
http://forums.shoutcast.com/showthread.php?t=95680
IntFmt only calls wsprintf, so you can get all the information you need about formatting from MSDN.
Could be very helpful for unexperienced NSIS users 🙂
Anders#
The problem with linking to MSDN is that Microsoft are unable to keep their URLs working for than a couple of years. If you look at the thread you linked to you will see a MSDN link but it is broken. I will add a link anyway.
PascalB#
Indeed, i did realize that asweel. Still, a note about the internal "wsprintf"-call and the MSDN would be helpful.
Thank you for your help and understanding.