Email Validation Function: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Function modified in order to be used as header)
m (Comment out unused label)
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:


This function is useful for checking the format of an email address. Then returning an error if it doesn't meet minimum requirements.<br>
This function is useful for checking the format of an email address. Then returning an error if it doesn't meet minimum requirements.<br>
''This function originally edited by user [http://forums.winamp.com/member.php?s=9adcee5fd428822e00fde45bc4d1561f&action=getinfo&userid=213274 99999999] and posted [http://forums.winamp.com/showthread.php?s=9adcee5fd428822e00fde45bc4d1561f&threadid=267283 on this forum thread]''
''This function originally edited by user [http://forums.winamp.com/member.php?action=getinfo&userid=213274 99999999] and posted [http://forums.winamp.com/showthread.php?threadid=267283 on this forum thread]''
<highlight-nsis>
<highlight-nsis>
  VAR $USEREMAILADDRESS
  VAR $USEREMAILADDRESS
Line 91: Line 91:
     Push $R4
     Push $R4
     Push $R5
     Push $R5
 
     #count the number of @'s more than one is invalid, less than one is invalid
     #count the number of @'s more than one is invalid, less than one is invalid
     ${WordFind} "$R0" "@" "*" $R1
     ${WordFind} "$R0" "@" "*" $R1
     StrCmp "1" "$R1" lbl_check2 lbl_error
     StrCmp "1" "$R1" lbl_check2 lbl_error
 
  lbl_check2:
  lbl_check2:
     #count the number of words delimited by @ it should be 2.
     #count the number of words delimited by @ it should be 2.
     ${WordFind} "$R0" "@" "#" $R1
     ${WordFind} "$R0" "@" "#" $R1
     StrCmp "2" "$R1" lbl_check3 lbl_error
     StrCmp "2" "$R1" lbl_check3 lbl_error
 
  lbl_check3:
  lbl_check3:
     #Split the words into user and domain
     #Split the words into user and domain
     ${WordFind} "$R0" "@" "0" $R2
     ${WordFind} "$R0" "@" "+1" $R2
     ${WordFind} "$R0" "@" "1" $R3
     ${WordFind} "$R0" "@" "-1" $R3
     #Determine if either of the fields contain special RFC822 characters
     #Determine if either of the fields contain special RFC822 characters
     ${StrFilter} "$R2" "" "" '()<>,;:\"[]' $R1
     ${StrFilter} "$R2" "" "" '()<>,;:\"[]' $R1
Line 110: Line 110:
     ${StrFilter} "$R3" "" "" '()<>,;:\"[]' $R1
     ${StrFilter} "$R3" "" "" '()<>,;:\"[]' $R1
     StrCmp "$R3" "$R1" lbl_check4 lbl_error
     StrCmp "$R3" "$R1" lbl_check4 lbl_error
 
  lbl_check4:
  lbl_check4:
     #Determine the number of fields in user and domain, check to see
     #Determine the number of fields in user and domain, check to see
     #the number of delimiter is one less than the number of words.
     #the number of delimiter is one less than the number of words.
    StrCpy $R4 0
    StrCpy $R5 0
     ${WordFind} "$R2" "." "*" $R4
     ${WordFind} "$R2" "." "*" $R4
     ${WordFind} "$R2" "." "#" $R5
     ${WordFind} "$R2" "." "#" $R5
   
    StrCmp "$R4" "$R2" lbl_check5
    StrCmp "$R5" "$R2" lbl_check5
   
     IntOp $R4 $R4 + 1
     IntOp $R4 $R4 + 1
     StrCmp "$R4" "$R5" 0 lbl_error
     StrCmp "$R4" "$R5" 0 lbl_error
   
lbl_check5: 
    StrCpy $R4 0
    StrCpy $R5 0   
     ${WordFind} "$R3" "." "*" $R4
     ${WordFind} "$R3" "." "*" $R4
     ${WordFind} "$R3" "." "#" $R5
     ${WordFind} "$R3" "." "#" $R5
   
    StrCmp "$R3" "$R4" lbl_error
    StrCmp "$R3" "$R5" lbl_error
   
     IntOp $R4 $R4 + 1
     IntOp $R4 $R4 + 1
     StrCmp "$R4" "$R5" lbl_check5 lbl_error
     StrCmp "$R4" "$R5" 0 lbl_error
 
  lbl_check5:
  ; Unused label? lbl_check6:
     # make sure there is at least one "." in the domain section.
     # make sure there is at least one "." in the domain section.
     ${WordFind} "$R3" "." "*" $R1
     ${WordFind} "$R3" "." "*" $R1
     IntCmp 1 $R1 lbl_end lbl_end lbl_error
     IntCmp 1 $R1 lbl_end lbl_end lbl_error
    goto lbl_end
StrCpy $R0 0


  lbl_error:
  lbl_error:
     StrCpy $R0 1
     StrCpy $R0 1
 
  lbl_end:
  lbl_end:
     Pop $R5
     Pop $R5

Latest revision as of 15:39, 25 April 2019

Author: 99999999 (talk, contrib)


CheckUserEmailAddress original version

This function is useful for checking the format of an email address. Then returning an error if it doesn't meet minimum requirements.
This function originally edited by user 99999999 and posted on this forum thread

 VAR $USEREMAILADDRESS
 Function CheckUserEmailAddress
    StrCpy $R1 $USEREMAILADDRESS
    StrCpy $R2 ""
    StrCpy $R3 ""
    #count the number of @'s more than one is invalid, less than one is invalid
    ${WordFind} "$USEREMAILADDRESS" "@" "*" $R1
    StrCmp "1" "$R1" lbl_check2 lbl_error
 lbl_check2:
    #count the number of words delimited by @ it should be 2.
    ${WordFind} "$USEREMAILADDRESS" "@" "#" $R1
    StrCmp "2" "$R1" lbl_check3 lbl_error
 lbl_check3:
    #Split the words into user and domain
    ${WordFind} "$USEREMAILADDRESS" "@" "0" $R2
    ${WordFind} "$USEREMAILADDRESS" "@" "1" $R3
    #Determine if either of the fields contain special RFC822 characters
    ${StrFilter} "$R2" "" "" '()<>,;:\"[]' $R1
    StrCmp "$R2" "$R1" 0 lbl_error
    ${StrFilter} "$R3" "" "" '()<>,;:\"[]' $R1
    StrCmp "$R3" "$R1" 0 lbl_error
 lbl_check4:
    #Determine the number of fields in user and domain, check to see 
    #the number of delimiter is one less than the number of words.
    ${WordFind} "$R2" "." "*" $R5
    ${WordFind} "$R2" "." "#" $R6
    IntOp $R5 $R5 + 1
    StrCmp "$R5" "$R6" 0 lbl_error
    ${WordFind} "$R3" "." "*" $R5
    ${WordFind} "$R3" "." "#" $R6
    IntOp $R5 $R5 + 1
    StrCmp "$R5" "$R6" lbl_check5 lbl_error
 lbl_check5:
    # make sure there is at least one "." in the domain section.
    ${WordFind} "$R3" "." "*" $R1
    IntCmp 1 $R1 lbl_end lbl_end lbl_error
    goto lbl_end
 lbl_error:
    SetErrors
 lbl_end:
 FunctionEnd

CheckUserEmailAddress modified version

And here is a modified by Red Wine version in order to be applied as an independent NSIS header.

The header

/********************CheckUserEmailAddress*********************
*                                                             *
*                Simple Email Parsing Code.                   *
*                                                             *
* Syntax:                                                     *
* ${CheckUserEmailAddress} "$user_input_var" "$result_var"    *
*                                                             *
* "$user_input_var":  User's input in custom dialog           *
* "$result_var"    :  $result_var=1  (invalid e-mail address) *
*                                                             *
* Example:                                                    *
*	${CheckUserEmailAddress} "$0" "$R0"                   *
*	  $R0="1"  user entered an invalid e-mail address     *
*                                                             *
***************************************************************/
 
!ifndef _CheckUserEmailAddress_NSH_
!define _CheckUserEmailAddress_NSH_
 
!include "WordFunc.nsh"
!insertmacro WordFind
!insertmacro StrFilter
 
Function CheckUserEmailAddress
 
    !define CheckUserEmailAddress "!insertmacro CheckUserEmailAddressCall"
 
    !macro CheckUserEmailAddressCall _INPUT _RESULT
	Push "${_INPUT}"
	Call CheckUserEmailAddress
	Pop ${_RESULT}
    !macroend
 
    Exch $R0
    Push $R1
    Push $R2
    Push $R3
    Push $R4
    Push $R5
 
    #count the number of @'s more than one is invalid, less than one is invalid
    ${WordFind} "$R0" "@" "*" $R1
    StrCmp "1" "$R1" lbl_check2 lbl_error
 
 lbl_check2:
    #count the number of words delimited by @ it should be 2.
    ${WordFind} "$R0" "@" "#" $R1
    StrCmp "2" "$R1" lbl_check3 lbl_error
 
 lbl_check3:
    #Split the words into user and domain
    ${WordFind} "$R0" "@" "+1" $R2
    ${WordFind} "$R0" "@" "-1" $R3
    #Determine if either of the fields contain special RFC822 characters
    ${StrFilter} "$R2" "" "" '()<>,;:\"[]' $R1
    StrCmp "$R2" "$R1" 0 lbl_error
    ${StrFilter} "$R3" "" "" '()<>,;:\"[]' $R1
    StrCmp "$R3" "$R1" lbl_check4 lbl_error
 
 lbl_check4:
    #Determine the number of fields in user and domain, check to see
    #the number of delimiter is one less than the number of words.
    StrCpy $R4 0
    StrCpy $R5 0
    ${WordFind} "$R2" "." "*" $R4
    ${WordFind} "$R2" "." "#" $R5
 
    StrCmp "$R4" "$R2" lbl_check5
    StrCmp "$R5" "$R2" lbl_check5
 
    IntOp $R4 $R4 + 1
    StrCmp "$R4" "$R5" 0 lbl_error
 
 lbl_check5:  
    StrCpy $R4 0
    StrCpy $R5 0    
    ${WordFind} "$R3" "." "*" $R4
    ${WordFind} "$R3" "." "#" $R5
 
    StrCmp "$R3" "$R4" lbl_error
    StrCmp "$R3" "$R5" lbl_error
 
    IntOp $R4 $R4 + 1
    StrCmp "$R4" "$R5" 0 lbl_error
 
 ; Unused label? lbl_check6:
    # make sure there is at least one "." in the domain section.
    ${WordFind} "$R3" "." "*" $R1
    IntCmp 1 $R1 lbl_end lbl_end lbl_error
 	StrCpy $R0 0
 
 lbl_error:
    StrCpy $R0 1
 
 lbl_end:
    Pop $R5
    Pop $R4
    Pop $R3
    Pop $R2
    Pop $R1
    Exch $R0
FunctionEnd
 
!endif

Usage example

/***************************************
*          EXAMPLE OF USAGE            *
****************************************/
!include "CheckUserEmailAddress.nsh"
!define CUST_INI "$PLUGINSDIR\custom.ini"
 
outfile 'test.exe'
 
page custom CreateCustom LeaveCustom
page instfiles
 
section -
;
sectionend
 
Function CreateCustom
    push $0
    InstallOptions::Dialog "${CUST_INI}"
    pop $0
    pop $0
FunctionEnd
 
Function LeaveCustom
    ReadINIStr $0 "${CUST_INI}" "field 2" "state"
 
    ${CheckUserEmailAddress} "$0" "$R0"
    StrCmp $R0 "1" 0 next
    MessageBox MB_OK "Invalid e-mail address, please try again."
    Abort 
 
 next:
FunctionEnd
 
Function .onInit
    InitPluginsDir
 
    WriteINIStr "${CUST_INI}" "Settings" "NumFields" "2"
 
    WriteINIStr "${CUST_INI}" "field 1" "type" "groupbox"
    WriteINIStr "${CUST_INI}" "field 1" "left" "30"
    WriteINIStr "${CUST_INI}" "field 1" "right" "-30"
    WriteINIStr "${CUST_INI}" "field 1" "top" "20"
    WriteINIStr "${CUST_INI}" "field 1" "bottom" "70"
    WriteINIStr "${CUST_INI}" "field 1" "text" "Please enter your e-mail address"
 
    WriteINIStr "${CUST_INI}" "field 2" "type" "text"
    WriteINIStr "${CUST_INI}" "field 2" "left" "50"
    WriteINIStr "${CUST_INI}" "field 2" "right" "-50"
    WriteINIStr "${CUST_INI}" "field 2" "top" "40"
    WriteINIStr "${CUST_INI}" "field 2" "bottom" "52"
    WriteINIStr "${CUST_INI}" "field 2" "state" "Please enter your e-mail address to continue"
FunctionEnd