Hello,
I have this program that will check and see if the email address the user entered contains valid characters and the @ and . character.
The only problem is that my program isn't working... When I enter a valid email address it says it is invalid, when I enter an invalid email address it also says it's invalid.
Here is my code:
procedure TForm1.btnValidateClick(Sender: TObject);
var
sEmail: string;
bResult: boolean;
i: integer;
iPos: integer;
const
ALLOWED = 'abcdefghijklmnopqrstuvwxyz0123456789_';
begin
sEmail := edtEmailAddress.Text;
i := 1;
bResult := false;
{ Check to see if there is an @ in the email address }
iPos := Pos('@', sEmail);
if iPos > 0 then
bResult := true;
else
bResult := false;
{ Check to see if there is an . in the email address }
iPos := Pos('.', sEmail);
if (iPos > 0) and (bResult = true)then
bResult := true;
else
bResult := false;
{ Check to see if all the chars are valid }
while i <= Length(sEmail) do
begin
if sEmail[i] in [ALLOWED[1] .. ALLOWED[Length(ALLOWED)]] then
bResult := true
else
begin
bResult := false;
break;
end;
Inc(i)
end;
if bResult = true then
pnlValid.Caption := 'Your email address is valid.'
else
pnlValid.Caption := 'Your email address is not valid.';
end;
I dont know what is wrong and have been strugling with it for about 3 hours now. Please help me fix this.