Force the user to put text in the inputquery.

Hi,

I have this procedure below where the user can set his password twice
in 2 inputqueries, that uses a function to make sure that the user can not
send an empty password. So it force the user to put something in the input-
queries.

procedure TPropertiesDlg.PswChkBxClick(Sender: TObject);
var
 InputStr1 : string;
 InputStr2 : string;
begin
 if PswChkBx.Checked then
  begin
  InputStr1 := GetInput('Password', 'Please enter new password:'); <========
   if InputStr1 <> '' Then
   begin
   InputStr2 := GetInput('Password', 'Please confirm new password:'); <========
   if InputStr2 <> '' then
   begin
       if InputStr1 <> InputStr2
        then showmessage('The confirmation password was not correct. Password is unchanged.')
        else
         begin
          if MainForm.SetPassword(InputStr2)
           Then showmessage('Password successfully changed.')
           Else ShowMessage('Password could not be changed!');
          Exit;
         end;
      end;
    end;
  end;
 MainForm.SetPassword('');
 PswChkBx.Checked:=False;
end;

function TPropertiesDlg.GetInput(aCaption, aPrompt: String): String;
var
  ok: Boolean;
begin
 Result := '';
 repeat
 PostMessage(Handle, InputBoxMessage, 0, 0);
  ok := InputQuery(aCaption, aPrompt, Result);
  if ok and (Result = '') THEN
   showmessage ('Please enter a password.');
 until(not ok or (Result <> ''));
end;

In this procedure below the programm looks if a database is protected
or not. If it does an inputquery appears where the user can enter
his/her password. But I want to use the same function in this procedure also.
So when the database is protected and the inputquery appears. The user can not
press the OK-button if the editfield of the inputquery is empty.

procedure TMainform.OpenDatabase;
var
 i: integer;
begin
 if not IsLogin('') then
  begin
   PostMessage(Handle, InputBoxMessage, 0, 0);
   if not IsLogin(InputBox('Password', 'Please enter password:', '')) then <=========
    begin
     showmessage('Invalid Password');
     Exit;
    end;
  end;
 with PageControl1 do for i:=Pagecount -1 downto 0 do pages[i].Free;  
 ADOTable1.Open;
 ADOTabs.Open;
  LoadTree;
end;

Who knows the answer and is willing to help me?

Greetings,

Peter Kiers
LVL 1
peterkiersAsked:
Who is Participating?
 
Emmanuel PASQUIERConnect With a Mentor Freelance Project ManagerCommented:
almost. It should work, but you don't need the PostMessage because it is done already in GetInput
procedure TMainform.OpenDatabase;
var
 i: integer;
begin
 if not IsLogin('') then
  begin
   if not IsLogin(GetInput('Password', 'Please enter password:')) then
    begin
     showmessage('Invalid Password');
     Exit;
    end;
  end;
 with PageControl1 do for i:=Pagecount -1 downto 0 do pages[i].Free;  
 ADOTable1.Open;
 ADOTabs.Open;
  LoadTree;
end;

Open in new window

0
 
Ioannis AnifantakisSoftware EngineerCommented:
To make sure the user doesn't put empty string you have to check spaces too, because thats not an empty string, so I suggest you trim it

change
   if InputStr1 <> '' Then
to
   if Trim(InputStr1) <> '' Then

Now if you don't want to press the OK button, have it initially disabled (mybutton.enabled:=false;)
When the trimmed passwords match then change (mybutton.enabled:=true;)

To have this change your "onkeypress" of your FORM to check the textfields if they match everytime you have a key pressed.  To make keypressed fire on the form regardless on what control you are typing you have to set the property of your form "keypreview" to true.

Other than that, I don't understand where your problem is.
0
 
Ioannis AnifantakisSoftware EngineerCommented:
Also to make it more clear you start with your "mybutton.enabled:=false" and the code on the "onkeypress" on your form should be like that:


procedure TMainForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
    myButton.enabled:=(Trim(inputStr1)=Trim(inputStr2));
end;

// or the more easy to understand but less code-optimised is this
procedure TMainForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
    if Trim(inputStr1)=Trim(inputStr2) then
        myButton.enabled:=true
    else
        myButton.enabled:=false;
end;

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
peterkiersAuthor Commented:
This is not what I want. My code works perfect. The only thing i want is the use
the same GetInput-function check in the OpenDatabase-procedure.

Peter
0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
you need an initialized instance of TPropertiesDlg before calling the OpenDataBase

let's say

frmPropertyDlg:TPropertiesDlg;


procedure TMainform.OpenDatabase;
var
 i: integer;
begin
 if not IsLogin('') then
  begin
   PostMessage(Handle, InputBoxMessage, 0, 0);
   if not IsLogin(frmPropertyDlg.GetInput('Password', 'Please enter password:')) then
    begin
     showmessage('Invalid Password');
     Exit;
    end;
  end;
 with PageControl1 do for i:=Pagecount -1 downto 0 do pages[i].Free;  
 ADOTable1.Open;
 ADOTabs.Open;
  LoadTree;
end;

Open in new window

0
 
Emmanuel PASQUIERFreelance Project ManagerCommented:
I would recommend that this GetInput function be a member of your main form instead of your property form to be sure it's available always.
0
 
peterkiersAuthor Commented:
Hi,

I have declared the same function in the mainform also.
So i quess it have to be like this:

procedure TMainform.OpenDatabase;
var
 i: integer;
begin
 if not IsLogin('') then
  begin
   PostMessage(Handle, InputBoxMessage, 0, 0);
   if not IsLogin(GetInput('Password', 'Please enter password:')) then
    begin
     showmessage('Invalid Password');
     Exit;
    end;
  end;
 with PageControl1 do for i:=Pagecount -1 downto 0 do pages[i].Free;  
 ADOTable1.Open;
 ADOTabs.Open;
  LoadTree;
end;
0
 
peterkiersAuthor Commented:
I have tested it and it works for me....

Greetings, Peter Kiers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.