views:

226

answers:

2

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  A: 

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.

Charles
+1  A: 

Correct me if I am wrong, but in addition to the answer by Charles, you need to use the check: directive on a line , no ?

As in :

Name: {userappdata}\Microsoft\Internet Explorer\Quick Launch\TheProgram ; _
    Filename: "{app}\bin\Sitestepper.exe"; _
    WorkingDir: {app}\bin; IconFilename: {app}\bin\Sitestepper.exe; _
    Comment: Sitestepper; IconIndex: 0; _
    Flags: createonlyiffileexists; _
    check:ValidateEmail; _
    Tasks:QuickLaunchIcon

Note the call to ValidateEmail on the check line.

Hope this puts you in the correct direction.

Edelcom
Actually, I was assuming the ValidateEmail function will be called other code within the script, for example, from the NextButtonClick event. If the result from ValidateEmail will instead indicate whether to install a file or not, than your code above can indeed be used.
Charles