1

I tried using an If statement , but it only shows the message if i only put @ in the edit

var
sname , email : string ;
iAge , igrade : integer ;
iVal : string;

begin
ival := '@';
email :=  (edtEmail.Text);
if ival = email then


 begin


Showmessage(' Email Address must contain @ ');
 end;
J...
  • 30,968
  • 6
  • 66
  • 143

2 Answers2

4

Indeed, the condition ival = email is true iff ival and email are identical strings. Since ival is @, the condition is true iff email is exactly @.

You want to check if @ is found inside email. To do this, you can use the Pos function, which returns the 1-based index of the first character of the first occurrence of a substring within a string, or 0 is the substring isn't found in the string:

if Pos('@', email) = 0 then
  ShowMessage('The email address must contain @.');

Notice that there really isn't any need for a variable to hold the at character.

In modern versions of Delphi, it is better to write

if not email.Contains('@') then
  ShowMessage('The email address must contain @.');

using TStringHelper.Contains, since this is easier to read.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
2

You can use Pos function to check if the @ character can be located in the email string.

if Pos('@', email) = 0 then
begin
   Showmessage(' Email Address must contain @ ');
end;

Here is an article about validating email addresses in Delphi

https://www.howtodothings.com/computers/a1169-validating-email-addresses-in-delphi.html

Charlie
  • 22,886
  • 11
  • 59
  • 90