Validation Function: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
(Added macro to make calling easier.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|dselkirk}}
|<small>Author: [[{{ns:2}}:dselkirk|dselkirk]] ([[{{ns:3}}:dselkirk|talk]], [[{{ns:-1}}:Contributions/dselkirk|contrib]])</small>
 
|}
<br style="clear:both;">
== Description ==
== Description ==
Allows you to validate a string with a custom criteria. This means you can validate a string to only contain numbers, or just letters, or a combination of both. Enjoy!
Allows you to validate a string with a custom criteria. This means you can validate a string to only contain numbers, or just letters, or a combination of both. Enjoy!
Line 8: Line 6:
== Example of Usage ==
== Example of Usage ==
<highlight-nsis>
<highlight-nsis>
Section
${Validate} $out "test" ${ALPHA}
  Push "This is a test"
;    $out contains 1
  Push "${ALPHA}"
${Validate} $out "test1" ${ALPHA}
   Call Validate
;    $out contains 0    
  Pop $0
${Validate} $out "123" ${NUMERIC}
  MessageBox MB_OK $0
;    $out contains 1
SectionEnd
${Validate} $out "123asdf" ${NUMERIC}
;    $out contains 0
 
 
</highlight-nsis>
</highlight-nsis>


== The Function ==
== The Function ==
<highlight-nsis>
<highlight-nsis>
!define Validate "!insertmacro Validate"
!macro Validate ResultVar String CharacterSet
Push "${String}"
Push "${CharacterSet}"
Call ValidateInternal
Pop "${ResultVar}"
!macroend
!define ALPHA "abcdefghijklmnopqrstuvwxyz "
!define ALPHA "abcdefghijklmnopqrstuvwxyz "
!define NUMERIC "1234567890"
!define NUMERIC "1234567890"
!define SPECIAL "~!@#$%^&*()_+|`\=-}{$\":?><][';/.,"
!define SPECIAL "~!@#$%^&*()_+|`\=-}{$\":?><][';/.," # workaround for syntax highlighting - '


;Push "value to check"
;Push "value to check"
;Push "comparisonlist"
;Push "comparisonlist"
Function Validate
Function ValidateInternal
   Push $0
   Push $0
   Push $1
   Push $1
Line 69: Line 81:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
[[Category:String Functions]]

Latest revision as of 13:42, 17 July 2012

Author: dselkirk (talk, contrib)


Description

Allows you to validate a string with a custom criteria. This means you can validate a string to only contain numbers, or just letters, or a combination of both. Enjoy!

Example of Usage

${Validate} $out "test" ${ALPHA}
;    $out contains 1
${Validate} $out "test1" ${ALPHA}
;    $out contains 0   
${Validate} $out "123" ${NUMERIC}
;    $out contains 1
${Validate} $out "123asdf" ${NUMERIC}
;    $out contains 0

The Function

!define Validate "!insertmacro Validate"
 
!macro Validate ResultVar String CharacterSet
	Push "${String}"
	Push "${CharacterSet}"
	Call ValidateInternal
	Pop "${ResultVar}"
!macroend
 
 
!define ALPHA "abcdefghijklmnopqrstuvwxyz "
!define NUMERIC "1234567890"
!define SPECIAL "~!@#$%^&*()_+|`\=-}{$\":?><][';/.," # workaround for syntax highlighting - '
 
;Push "value to check"
;Push "comparisonlist"
Function ValidateInternal 
  Push $0
  Push $1
  Push $2
  Push $3 ;value length
  Push $4 ;count 1
  Push $5 ;tmp var 1
  Push $6 ;list length
  Push $7 ;count 2
  Push $8 ;tmp var 2
  Exch 9
  Pop $1 ;list
  Exch 9
  Pop $2 ;value
  StrCpy $0 1
  StrLen $3 $2
  StrLen $6 $1
  StrCpy $4 0
  lbl_loop:
    StrCpy $5 $2 1 $4
    StrCpy $7 0
    lbl_loop2:
      StrCpy $8 $1 1 $7
      StrCmp $5 $8 lbl_loop_next 0
      IntOp $7 $7 + 1
      IntCmp $7 $6 lbl_loop2 lbl_loop2 lbl_error
  lbl_loop_next:
  IntOp $4 $4 + 1
  IntCmp $4 $3 lbl_loop lbl_loop lbl_done
  lbl_error:
  StrCpy $0 0
  lbl_done:
  Pop $6
  Pop $5
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch 2
  Pop $7
  Pop $8
  Exch $0
FunctionEnd