Old 12th August 2003, 00:45   #1
brodbeck
Junior Member
 
Join Date: Aug 2003
Location: Taquara, RS, Brazil
Posts: 2
Wink 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
brodbeck is offline   Reply With Quote
Old 12th August 2003, 11:14   #2
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Thanks.
Please post your script on the NSIS Archive, or do you wish me to post it?

-Stu
Afrow UK is offline   Reply With Quote
Old 12th August 2003, 12:37   #3
kichik
M.I.A.
[NSIS Dev, Mod]
 
kichik's Avatar
 
Join Date: Oct 2001
Location: Israel
Posts: 11,343
Another way of doing it is (will not work on numbers that are > 2147483648):

code:
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


NSIS FAQ | NSIS Home Page | Donate $
"I hear and I forget. I see and I remember. I do and I understand." -- Confucius
kichik 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