unit PasswordInputFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, UtilMethods;

type
  TPasswordInputForm = class(TForm)
    lePassword: TLabeledEdit;
    btnOk: TButton;
    btnCancel: TButton;
    imgError: TImage;
    lblWarning: TLabel;
    procedure lePasswordExit(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure lePasswordKeyPress(Sender: TObject; var Key: Char);
    procedure lePasswordKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    procedure CheckPassword;
  public
  end;

function InputPassword(var APassword: string): Boolean;

implementation

function InputPassword(var APassword: string): Boolean;
var
  InputForm: TPasswordInputForm;
begin
  InputForm := TPasswordInputForm.Create(nil);
  try
    if InputForm.ShowModal = mrOk then
    begin
      Result := True;
      APassword := InputForm.lePassword.Text;
    end
    else
      Result := False;
  finally
    InputForm.Free;
  end;
end;

{$R *.dfm}

procedure TPasswordInputForm.CheckPassword;
begin
  imgError.Visible := not CheckValidPassword(lePassword.Text);
  lblWarning.Visible := not CheckValidPassword(lePassword.Text);
end;

procedure TPasswordInputForm.lePasswordExit(Sender: TObject);
begin
  CheckPassword;
end;

procedure TPasswordInputForm.btnOkClick(Sender: TObject);
begin
  CheckPassword;
  if CheckValidPassword(lePassword.Text) then
    ModalResult := mrOk;
end;

procedure TPasswordInputForm.lePasswordKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not (Key in ['a'..'z', 'A'..'Z', '0'..'9', #8, #13]) then
    Key := #0;
end;

procedure TPasswordInputForm.lePasswordKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key = 13 then
    btnOk.Click;
end;

end.
