Archive: Function to verify if string is numeric


Function to verify if string is numeric
To all members:

I've searched in this forum and doesn't found. Then I wrote this function to verify is a string is numeric. This is usefull to check serial number during installation.

Enjoy!

;IsNumeric
;
; Verify if the string is numeric
; input, top of stack string
; output, top of stack '1' or '0'
;
; Usage:
; Push $x ;string to verify
; Call IsNumeric
; Pop $x ;result '1' or '0'
;
Function IsNumeric
Exch $R0
Push $R1
Push $R2
Push $R3
StrCpy $R1 0
StrCpy $R3 "0" ;Set result not numeric
loop:
StrCpy $R2 $R0 1 $R1
IntOp $R1 $R1 + 1
StrCmp $R2 "" endLoop
StrCmp $R2 "0" loop
StrCmp $R2 "1" loop
StrCmp $R2 "2" loop
StrCmp $R2 "3" loop
StrCmp $R2 "4" loop
StrCmp $R2 "5" loop
StrCmp $R2 "6" loop
StrCmp $R2 "7" loop
StrCmp $R2 "8" loop
StrCmp $R2 "9" loop
;If char R$2 is not numeric, then just go out,
;because $R0 is already set not numeric
Goto done

endLoop:
;Clean initial result, because the end of string was reached
StrCpy $R3 "1"

done:
StrCpy $R0 $R3
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd


Thanks.
Please post your script on the NSIS Archive, or do you wish me to post it?

-Stu


Another way of doing it is (will not work on numbers that are > 2147483648):

StrCpy $0 "001456"

StrCpy $3 -1
loop:
IntOp $3 $3 + 1
StrCpy $4 $0 1 $3
StrCmp $4 "0" loop loopend

loopend:
StrCpy $3 $0 "" $3

StrLen $2 $0
IntFmt $1 "%0$2d" $3
StrCmp $1 $0 is_number isnt_number