Email Validation Function

From NSIS Wiki
Jump to navigationJump to search

CheckUserEmailAddress

This function is useful for checking the format of an email address. Then returning an error if it doesn't meet minimum requirements.

 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