Basic email validation within Inno Setup script

2019-05-26 13:10发布

问题:

I'm wanting to do a basic string validation within an Inno Setup script to be relatively certain the string is an email address. I just want to see that there is a '@' character followed by a '.' character and that there is at least one character on either side of these. Something similar to this regular expression:

[^@]+@.+\.[^\.]

The lack of regular expressions and limited string functions available in object pascal are causing me grief. It would be simple enough to reverse the string, find the first '.' and '@' and then do some comparisons, but there's no Reverse(string) function available.

I know I can call an exported function from a helper DLL I write, but I was hoping to avoid this solution.

Any other suggestions?

回答1:

An excellent question! Allow me to suggest an answer...

function ValidateEmail(strEmail : String) : boolean;
var
    strTemp  : String;
    nSpace   : Integer;
    nAt      : Integer;
    nDot     : Integer;
begin
    strEmail := Trim(strEmail);
    nSpace := Pos(' ', strEmail);
    nAt := Pos('@', strEmail);
    strTemp := Copy(strEmail, nAt + 1, Length(strEmail) - nAt + 1);
    nDot := Pos('.', strTemp) + nAt;
    Result := ((nSpace = 0) and (1 < nAt) and (nAt + 1 < nDot) and (nDot < Length(strEmail)));
end;

This function returns true if there are no spaces in the email address, it has a '@' followed by a '.', and there is at least one character on either side of the '@' and '.'. Close enough for government work.