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
Peter KiersOperatorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis AnifantakisProgramming InstructorCommented:
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 AnifantakisProgramming InstructorCommented:
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
Peter KiersOperatorAuthor 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
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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
Peter KiersOperatorAuthor 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
Emmanuel PASQUIERFreelance 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KiersOperatorAuthor Commented:
I have tested it and it works for me....

Greetings, Peter Kiers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.