|
| Validate email address | 
|
|---|
Product: Delphi 3.x (or higher) | Category: Internet / Web | Skill Level:
 | Scoring:  | Last Update: 06/13/2002 | Search Keys: delphi delphi3000 article borland vcl code-snippet validation email address | Times Scored: 21 | Visits: 13335 | Uploader: Udo Nesshoever Company: | Reference: N/A | | | Question/Problem/Abstract:
How to validate an email address | Answer:
This solution does better validation than article 1067 (http://www.delphi3000.com/article.asp?ID=1067) but is smaller (at least no component) than article 1346 (http://www.delphi3000.com/article.asp?ID=1346).
Updated on 2000-12-06 due to Phil's and Sven's comments.
Updated on 2002-06-13 due to Carlos' comment.
Thanks for improving!
function IsValidEmail(const Value: string): boolean;
function CheckAllowed(const s: string): boolean;
var
i: integer;
begin
Result:= false;
for i:= 1 to Length(s) do
begin
// illegal char in s -> no valid address
if not (s[i] in ['a'..'z','A'..'Z','0'..'9','_','-','.']) then
Exit;
end;
Result:= true;
end;
var
i: integer;
namePart, serverPart: string;
begin // of IsValidEmail
Result:= false;
i:= Pos('@', Value);
if (i = 0) or (pos('..', Value) > 0) then
Exit;
namePart:= Copy(Value, 1, i - 1);
serverPart:= Copy(Value, i + 1, Length(Value));
if (Length(namePart) = 0) // @ or name missing
or ((Length(serverPart) < 4)) // name or server missing or
then Exit; // too short
i:= Pos('.', serverPart);
// must have dot and at least 3 places from end
if (i < 2) or (i > (Length(serverPart) - 2)) then
Exit;
Result:= CheckAllowed(namePart) and CheckAllowed(serverPart);
end;
|
|