title: Data validation problems in Delphi
body: <p>I have a form with several labeled edits and radio buttons (grouped in radio groups) for the user to enter information, and a Save button to save the entries.  The Save button also closes the form.  If there are invalid entries (e.g., an alpha character instead of a number) or if some entries are left blank, I want to alert the user to correct the errors, and not close the form until there are no blank edit boxes or unchecked radio groups.  I do an error check separately for each edit box and radio group by means of raising a messageDlg alerting the user to the mistake.  However, if the user does not correct a mistake and tries to save the form, I want to keep the form open (not be able to close it) and alert the user to the mistake.  He should be able to close the form only after all the entries are valid and there are no unchecked radio groups.  </p>
  
  <p>Based on some replies to my earlier message, I have written the following code.  The first one is an example of data entry validation for one edit box (there are several such) and the second is the code for the OnClick event of my Save button.</p>
  
  <pre><code>procedure TfrmAnalysisOptions.lbleConstraintsMaxChange(Sender: TObject);
  var
    I: integer;
    Val, ValidEntry: string;
    Chr: char;
    RangeMin, RangeMax: Double;
    const Allowed = ['0'..'9', '.'];
  begin
  
    Val := lbleConstraintsMax.Text;
  
        //initialize values    
    ValidEntry := '';
    ConstraintsMaxChange := '';
  
    //value can contain only numerals, and "."
      for I := 1 to Length(Val) do
       begin
         Chr := Val[I];
         if not (Chr in Allowed) then
         begin
       MessageDlgPos('The value entered for the max value of the ' +
                 'constraint must contain only a numeral, a decimal ' +
                 'point or a negative sign.',
              mtError, [mbOK], 0, 300, 300);
       Exit;
  
         end
         else ValidEntry := 'OK'; //validity check for this part
  
       end;
  
       //max value cannot be zero or less than the min value
      if not TryStrToFloat(Val, RangeMax) then Exit
      else if RangeMax = 0 then
      begin
         MessageDlg('Max value cannot be zero.', mtError, [mbOK], 0);
         Exit;
      end
      else if not TryStrToFloat(lbleConstraintsMin.Text, RangeMin) then Exit
      else if RangeMax &lt; RangeMin then
        begin
      MessageDlgPos('Max value cannot be less than Min value.',
             mtError, [mbOK], 0, 300, 300);
      Exit;
        end
  
      else if (RangeMax &lt; 0) then
        begin
      MessageDlgPos('A constraint cannot be negative.',
                mtError, [mbOK], 0, 300, 300);
      Exit;
        end
  
      //final validity check
      else if ValidEntry = 'OK' then ConstraintsMaxChange := 'OK'
      else MessageDlgPos('There was an unexpected problem with the ' +
                 'value entered in the max constraints box.',
              mtError, [mbOK], 0, 300, 300);
  
  end;
  </code></pre>
  
  <p>And this is the code for the OnClick event of the Save button.  As you can see I am checking for the validity of several entries - and only if all of them are valid (that is have an 'OK' as the value of the corresponding variable), I allow the form to be closed.  it is raising the messageDlg correctly when there is an invalid entry. However, it is continuing to raise the messageDlg even after the error is corrected and the Save button is pressed.</p>
  
  <pre><code>procedure TfrmAnalysisOptions.btnSaveOptionsClick(Sender: TObject);
  //save input and output options
  begin
      //check if all the option questions have been answered
      if not ((ConstraintsMinChange = 'OK') and //validation of correct entry as it is being entered
      (ConstraintsMinExit = 'OK') and //validation of entry as user is moving to another entry after an incorrect entry
      //several other such 'OK's 
        then
        begin
      MessageDlgPos('There is an invalid entry on the form.  Please ' +
                'correct it.', mtError, [mbOK], 0, 300, 300);
      Exit;
        end
  
  
      else if //more messageDlgs if some other conditions are not met 
  
      else
        begin
        //save input and output options
        end;
       //finally if all the conditions are met, close the form 
      Close;
  end;
  </code></pre>