How to check for a valid EMail Address format in Delphi
Now a days most of applications have facility to send Email to Users with some information. So before send Email we generally check for valid Email address format. Here I have 2 approaches how to validate an Email address format in Delphi.
1. Validating Email addresses format using Regular Expression
A regular expression is a special text that we can use as a search pattern. So for this we can use a regular expression string to check Email address syntax. And Delphi XE or later versions provide TRegEx class to use regular expression functions. So we can use this approach only on Delphi XE or latter versions. But for this we have to use System.RegularExpressions unit in Uses part.
....
function IsMatch(const Input, Pattern: string): boolean;
function IsValidEmailRegEx(const EmailAddress: string): boolean;
....
function TForm1.IsMatch(const Input, Pattern: string): boolean;
begin
Result := TRegEx.IsMatch(Input, Pattern);
end;
function TForm1.IsValidEmailRegEx(const EmailAddress: string): boolean;
const
EMAIL_REGEX = '^((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])'
+'[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)'
+'(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])'
+'[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]'
+'{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d))'
+'{4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\'
+'[\x01-\x7f])+)\])(?(angle)>)$';
begin
Result := IsMatch(EmailAddress, EMAIL_REGEX);
end;
....
Use of above functions....
procedure TForm1.btnValidateEmailregxClick(Sender: TObject);
begin
if IsValidEmailRegEx(EditEmail.Text) then
ShowMessage('Valid Email Address')
else
ShowMessage('InValid Email Address');
end;
2. To check Email address format manually with a user defined function in Delphi
In this part I have created a function in Delphi to check all characters of the Email address that the character is a valid character for Email or not.
.....
function IsValidEmail(const Value: string): boolean;
.....
function TForm1.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 - 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
Result:= False;
i:= Pos('@', Value);
if (i = 0) then
Exit;
if(pos('..', Value) > 0) or (pos('@@', Value) > 0) or (pos('.@', Value) > 0)then
Exit;
if(pos('.', Value) = 1) or (pos('@', Value) = 1) then
Exit;
namePart:= Copy(Value, 1, i - 1);
serverPart:= Copy(Value, i + 1, Length(Value));
if (Length(namePart) = 0) or (Length(serverPart) < 5) then
Exit; // too short
i:= Pos('.', serverPart);
// must have dot and at least 3 places from end
if (i=0) or (i>(Length(serverPart)-2)) then
Exit;
Result:= CheckAllowed(namePart) and CheckAllowed(serverPart);
end;
.....
procedure TForm1.btnValidateEmailClick(Sender: TObject);
begin
if IsValidEmail(EditEmail.Text) then
ShowMessage('Valid Email Address')
else
ShowMessage('InValid Email Address');
end;
Please note that 1st approach will not work on Delphi versions before Delphi XE because handling of regular expression functions are introduced from Delphi XE. So for Delphi 2010, 2006, 7, 6 or lower versions use 2nd approach to validate a Email address.
Thanks for this!
ReplyDeletethank you
ReplyDeleteIt is really good function. Thanks for this.
ReplyDeleteI think we do need little bit improve of this function (IsValidEmail).
if (pos('..', Value) > 0) or
(pos('@@', Value) > 0) or
(pos('.@', Value) > 0) or
(pos('@.', Value) > 0) then
Exit;
if(Value[Length(Value)] = '.') or (Value[Length(Value)] = '@') then
Exit;
How can I upgrade my Delphi software
ReplyDeleteThanks for sharing this well written email checker online in Delphi
ReplyDeleteSorry. Can't say "thanks". MN@abcLOL.com is not working ;-)
ReplyDeleteDelphi email address
ReplyDelete
ReplyDeleteI think the admin of this website is really working hard for his site, because here every information is quality based information.
Email Validation Api- Antideo
It’s hard to search out educated folks on this topic, but you sound like you know what you’re speaking about! Thanks
ReplyDeleteFree Email Validator
Thanks for your write-up. What I want to point out is that while searching for a good on-line electronics go shopping, look for a internet site with complete information on critical indicators such as the level of privacy statement, safety details, payment guidelines, along with terms as well as policies. Always take time to read the help and also FAQ areas to get a greater idea of how the shop operates, what they can do for you, and how you can use the features.
ReplyDeleteEmail Address Checker
In addition to restrictions on syntax, there is a length limit on email addresses. That limit is a maximum of 64 characters (octets) in the "local part" (before the "@") and a maximum of 255 characters (octets) in the domain part (after the "@") for a total length of 320 characters.
ReplyDeleteYou should study RFC3696 before claiming to provide ways to check for valid email addresses.
ReplyDeleteif not (s[i] in ['a'..'z','A'..'Z','0'..'9','_','-','.','+']) then Exit;
is simply incorrect.
Is an email a Char or String
ReplyDeleteAccording to wikipedia https://en.wikipedia.org/wiki/Email_address local domain name with no TLD e.g. admin@mailserver1 is valid email address, however your function says it is not.
ReplyDeleteif the email starts with dash "-" this function is not working.
ReplyDelete