如何修复此语法错误。验证是主题

问题描述 投票:0回答:1

我正在编写的程序应该验证学生 ID(字符串)。 ID应该是ID的String[1]、[2]、[3]的位置应该是

String[1] = 'c', String[2] = 'f', and String[3] = 'h'
。 ID 应以“cfh”开头。这是完整的按钮事件处理程序代码:

procedure Tfrmmain.btnvalClick(Sender: TObject);
var ver: boolean;
    i, buttonSelected: integer;
begin
  ver := true;
  stID := edtID.Text;
  i := length(stID);

  //eRROR code 000
  if edtID.text = '' then begin errormessagelbl.caption := 'Enter a Student ID';
    errormessagelbl.Font.Color := clred;
    edtID.SetFocus;
      {if edtID.SetFocus = true then errormessagelbl.caption := '';}
    exit;
  end;

  //eRROR code 001
  if i <> 13 then begin beep;
    ver := false;
      if not ver then begin errormessagelbl.caption := 'Invalid ID: 001';
        errormessagelbl.Font.Color := clred;
        exit;
      end;

    edtID.text := '';
    edtID.SetFocus;
    exit;
  end;

  //eRROR code 002
  if (stID[1] = 'c') AND (stID[2] = 'f') then begin errormessagelbl.caption := 'Invalid ID: 002'; //focus line
    errormessagelbl.Font.Color := clred;
    exit;
  end;


  buttonSelected := MessageDlg('Are you sure that' + '''s correct!',mtConfirmation, mbYESNO, 0);
  if buttonSelected = mrYES then application.Terminate;
  if buttonSelected = mrNO then begin edtID.text := '';
    edtID.SetFocus;
  end;
end;

我尝试用

if NOT (stID[1] = 'c') OR (stID[2] = 'f') OR (stID[3] = 'h') then begin errormessagelbl.caption := 'Invalid ID: 002';
替换焦点线。

validation if-statement delphi syntax-error
1个回答
0
投票

我想你想要这样的东西?

function Validate(AText : string; var AErrorText : string) : integer;
begin
  Result := -1;

  if AText.IsEmpty then
    Result := 0;
  if AText.Length <> 13 then
    Result := 1;
  if not AText.StartsWith('cfh') then
    Result := 2;
  case Result of
    0: AErrorText := 'Enter a Student ID';
    1,2: AErrorText := Format('Invalid ID: 00%d', [Result]);
  end;
end;

procedure TForm3.btnvalClick(Sender: TObject);
var
  errorText: string;
begin
  if Validate(edtID.Text, errorText) >= 0 then begin
    errormessagelbl.Caption := errorText;
    errormessagelbl.Font.Color := clred;
    edtID.SetFocus;
  end else begin
    if MessageDlg('Are you sure that''s correct!',mtConfirmation, mbYESNO, 0) = mrYes then begin
      stID := edtID.Text;
      Application.Terminate
    end else begin
      edtID.text := '';
      edtID.SetFocus;
    end;
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.