unit UMailAddressCheck;
interface
uses Windows, Classes, Registry, SysUtils, StrUtils, Dialogs, Forms;
function IsVaildAllEmailAddr(EmailAddr: string): boolean;
implementation
function DelimitBySemicolon(const Str: string;Postion: integer): string;
var
ts:Tstringlist;
begin
Result :='';
ts := Tstringlist.Create;
ts.Delimiter:=';';
ts.DelimitedText:=Str;
Result := ts.Strings[Postion];
ts.Free;
end;
function IsVaildEmailAddr(EmailAddr: string): boolean;
var
Number, I: integer; //Number用于给字符'@'计数
TempStr: string;
begin
if Length(EmailAddr)=0 then
begin
Result:=True;
Exit;
end;
TempStr := EmailAddr;
Number := 0;
for I := 1 to Length(TempStr) do
begin
if (TempStr[I] = '@') then
INC(Number);
end;
if ((Number = 0) or (Number > 1)) then
Result := False //如果不含有字符'@',或者其个数大于1,则不合法
else
begin
if ((TempStr[1] = '@') or (TempStr[length(TempStr)] = '@')) then
Result := False //如果字符'@'的位置在字符串开头或者末尾,则不合法
else
begin
I := pos('@', TempStr); //获取字符'@'在字符串当中的位置
delete(TempStr, 1, I); //获取字符串中字符'@'后面的剩余子串
if (Length(TempStr) < 3) then
Result := False //如果剩余子串的长度小于3,则不合法
else
begin
if ((pos('.', TempStr) = 0) or (pos('.', TempStr) = length(TempStr))
or (pos('.', TempStr) = 1)) then
Result := False //如果剩余的子串当中不含有字符'.',或者其位置在//子串的开头或者末尾,则不合法
else
Result := True; //以上的判断都通过,则表示地址字符串为合法
end;
end;
end;
end;
function IsVaildAllEmailAddr(EmailAddr: string): boolean;
var
ts:Tstringlist;
EmailCount,I:integer;
MailAddr,MsgStr:string;
begin
Result:=True;
ts := Tstringlist.Create;
ts.Delimiter:=';';
ts.DelimitedText:=EmailAddr;
EmailCount:= ts.Count ;
ts.Free;
for I:= 0 to EmailCount-1 do
begin
MailAddr:= DelimitBySemicolon(EmailAddr,i);
if not IsVaildEmailAddr(MailAddr) then
begin
MsgStr:=Format('你输入的邮件地址%s不合法',[MailAddr]);
MessageDlg(MsgStr, mtInformation, [mbOk], 0);
Result:=False;
Exit;
end;
end;
end;
end.
检查输入的邮件地址是否合法
最新推荐文章于 2018-12-29 09:35:00 发布