



Veja como você pode validar um e-mail de forma muito simples!
Segue o código fonte da função:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function IsValidEmail(const Value: string): Boolean; function CheckAllowed(const s: string): Boolean; var i: Integer; begin Result := False; for i := 1 to Length(s) do if not(s[i] in ['a' .. 'z', 'A' .. 'Z', '0' .. '9', '_', '-', '.']) then Exit; Result := true; end; var i: Integer; NamePart, ServerPart: string; begin Result := False; i := Pos('@', Value); if i = 0 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; i := Pos('.', ServerPart); if (i = 0) or (i > (Length(ServerPart) - 2)) then Exit; Result := CheckAllowed(NamePart) and CheckAllowed(ServerPart); end; |
Exemplo de uso:
1 2 3 4 5 6 7 8 9 10 11 12 | procedure TForm1.Button1Click(Sender: TObject); begin if IsValidEmail('[email protected]') then ShowMessage('O E-mail é valido!') else ShowMessage('O E-mail NÃO é valido!'); if IsValidEmail('pedro.com.br') then ShowMessage('O E-mail é valido!') else ShowMessage('O E-mail NÃO é valido!') end; |