1214 Votes

Delphi/Lazarus: Password Edit

Tip by Stefan Trost | Last update on 2023-01-10 | Created on 2012-12-28

If you want to implement a login area or any other password input in your Delphi or Lazraus program, it is handy to design the box for entering the password like a password input field is typically designed in the browser: That is, the input characters are not displayed in clear text. Instead asterisks or dots are shown.

For the realization of such a project, we need no special component in Delphi as well as in Lazarus. We can just take our normal TEdit to mask the user input, that provides already the property innately "PasswordChar":

// normal display in plain text
Edit1.PasswordChar := #0;
 
// input is masked with *
Edit1.PasswordChar := '*';
 
// input is masked with a point
Edit1.PasswordChar := #180;

You can easily set this property by using the Object Inspector or the code above.

The character #0 (which is also the default value for "PasswordChar") ensures that the input is shown in plain text. All other characters except #0 lead that this specified character is displayed instead of the characters entered. In the example code, we show this for the asterisk '*' and the center point (that has the value #180 while we write the asterisk directly in the code as such here). Of course, you can also use any other character you want to use as a mask.

Setting, Reading and Processing of the Text

Incidentally, internally and in the code, the value of the Edit field can be easily addressed, set, read out and processed normally as usual with using Edit1.Text. The masking character is only displayed to the user, this display has no effect on the processing of the value.

Function to Toggle the Password Visibility

Some password inputs enable the user to make his password visible via a button (for a short time). We can also set up such a function very easily in our Delphi or Lazarus program:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.PasswordChar = #0 then begin
     Edit1.PasswordChar := '*';
  end else begin
     Edit1.PasswordChar := #0;
  end;
end;      

If the user clicks on this button, it is first checked whether the edit field is currently masked or not, that is whether a PasswordChar is set. If not, a PasswordChar is set (here the asterisk). If so, the current PasswordChar is revoked by setting it to #0. This allows the masking to be switched on and off.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

The Secure Password

Info | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.