提问人:Litha Socenywa 提问时间:8/21/2023 最后编辑:beerwinLitha Socenywa 更新时间:8/21/2023 访问量:93
如何修复此语法错误。验证是主题
How do I fix this syntax error. Validation is the topic
问:
我正在编写的程序应该验证学生 ID(字符串)。ID 应为 String[1]、[2] 的位置,ID 的 [3] 应为 。ID 应以“cfh”开头。下面是完整的按钮事件处理程序代码:String[1] = 'c', String[2] = 'f', and String[3] = 'h'
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';
答:
0赞
Oleksandr Morozevych
8/21/2023
#1
我想你想要这样的东西吗?
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;
评论
if (stID[1] <> 'c') OR (stID[2] <> 'f') OR (stID[3] <> 'h') then