Here is a function that you can use to check whether a given string is a valid email address in Delphi:
1 2 3 4 5 6 7 |
function IsValidEmail(const Email: string): Boolean; var EmailRegex: TRegEx; begin EmailRegex := TRegEx.Create(‘^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$’); Result := EmailRegex.IsMatch(Email); end; |
This function uses a regular expression to validate the email address. The regular expression checks for the following characteristics:
- The email address must contain a local part (the part before the ‘@’ symbol) and a domain part (the part after the ‘@’ symbol).
- The local part must contain at least one alphanumeric character and can also contain the characters ‘.’, ‘_’, ‘+’, and ‘-‘.
- The domain part must contain at least one alphabetic character, followed by a ‘.’ character, followed by at least one more alphabetic character.
You can use this function in your Delphi code like this:
1 2 3 4 |
if IsValidEmail(‘test@example.com’) then ShowMessage(‘Valid email address’) else ShowMessage(‘Invalid email address’); |
This will display a message indicating whether the email address ‘test@example.com‘ is valid or not.
Leave a Reply