Old 25th September 2019, 09:23   #1
PascalB
Junior Member
 
Join Date: Jul 2019
Posts: 7
Question 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:
code:

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
PascalB is offline   Reply With Quote
Old 25th September 2019, 17:57   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,175
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.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 25th September 2019, 22:54   #3
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,175
Quote:
Var /Global Input
!include LogicLib.nsh
StrCpy $Input 091

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

IntFmt $0 "%.4u" 1$Input
${If} $0 != 1$Input
Abort "Invalid"
${EndIf}

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 30th September 2019, 13:13   #4
PascalB
Junior Member
 
Join Date: Jul 2019
Posts: 7
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
https://docs.microsoft.com/en-us/win...user-wsprintfa

because my research found a post here (from 2002)
http://forums.shoutcast.com/showthread.php?t=95680
Quote:
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
PascalB is offline   Reply With Quote
Old 1st October 2019, 11:17   #5
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,175
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.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 1st October 2019, 15:16   #6
PascalB
Junior Member
 
Join Date: Jul 2019
Posts: 7
Thumbs up

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.
PascalB is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump