Validation Function

From NSIS Wiki

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
Personal tools
donate