Basic email validation within Inno Setup script

試著忘記壹切 提交于 2020-01-02 18:26:00

问题


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.



来源:https://stackoverflow.com/questions/2001255/basic-email-validation-within-inno-setup-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!