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.

Id take a hack at it (putting it on my PC and testing it) but is this Pascal or Delphi? Looks like Delphi...

commented: Yes it is delphi. +0

You don't account for dots and @ in the allowed characters.

Apart from that, you should check whether the email contains exactly one @, and the position(s) of the dot(s) matter too.

It is Delphi 7.

@pritaeas ive updated it to look like this: ALLOWED = 'abcdefghijklmnopqrstuvwxyz0123456789_@.';

but it still says its invalid email.

Perhaps accidentally inputted UPPERCASE characters? It doesn't account for those either. Which email address did you try to validate?

Step through it with the debugger so you can see where it fails. I think it's your in check. Try replacing it with a Pos

B.t.w. here's an interesting read.

OK, from debugging I see your problem is in the while....

Debugging some more as I havent done Pascal in a LONG time.

No, im not putting in uppercase letters.

@riahc3 thanks, apreciate any help I can get.

if sEmail[i] in [ALLOWED[1] .. ALLOWED[Length(ALLOWED)]] then 

I cant find any documentation on a "if.......in......then" statement.

Here is a quickie:

//declare another integer called x
while i <= Length(sEmail) do

  begin

    while x <= Length(ALLOWED) do
     begin

       if sEmail[i] = ALLOWED[x] then
         begin
               bResult := true;
               break;
         end;
       else
       begin
             bResult := false;
     // break;
         end;
           Inc(x) ;
     end;  
     Inc(i)

  end; 

Code is as-is and untested. Also very dirty and there are 1000000s of better ways to do this.

BTW, pritaeas is right: With this you are just validating if it is a email, not a proper email.

Thanks, yes i know its not a propper email. this is just the 'demo' for now.

Although the above is fine, you can also use Pos:

while i <= Length(sEmail) do
begin
  if Pos(sEmail[i], ALLOWED) = 0 then
  begin
    bResult := false;
    break;
  end;

  Inc(i);
end;

@pritaeas, Yes thanks man. I was just about to post my code that I came up with that would fix this problem. Mine is just a little different but does the same thing.

Thanks to both of you.

Why don't you upgrade to delphi 2010?? it's much more user friendly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.