Delphi验证字母和字符串

时间:2016-05-27 15:02:51

标签: validation delphi

Function GetPlayersName: String;
Var
  PlayerName: String;
  i: Integer;
Begin
  Write('What is your name? ');
  Readln(PlayerName);

  for i := 1 to length(PlayerName) do
  begin
    PlayerName[i] := Upcase(PlayerName[i]);
    if not(PlayerName[i] in ['A' .. 'Z']) AND (PlayerName[i] <> ' ') then
    begin
      writeln('Letters and spaces please');
    end;
  end
else
  GetPlayersName := PlayerName;
end;

出于某种原因,在上述验证期间,else语句将无法编译。以下是错误消息:

  

[dcc32错误] AQA_Reverse.dpr(125):预期E2029'结束'但发现'ELSE'

除此之外,当打印消息时,它会按字符数打印,我不知道如何更改它。

2 个答案:

答案 0 :(得分:2)

错误消息是因为您的DECLARE @jobdate datetime = '5/30/2016' SELECT c.item, c.qty_ordered AS qty_expected, j1.job, j1.suffix, Sum(j1.qty_released) AS sum_qty_released, Sum(j1.qty_complete) AS sum_qty_complete, (Sum(j1.qty_complete)-c.qty_ordered) AS expected_variance, (Sum(j1.qty_complete)-Sum(j1.qty_released)) AS regular_variance, j1.whse STUFF( SELECT DISTINCT '' + j2.job FROM job AS j2 WHERE j1.job = j2.job AND j1.suffix = j2.suffix FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,0,'') data FROM coitem AS c LEFT OUTER JOIN job AS j1 ON c.item = j1.item AND DATEADD(d, -6, c.due_date) = j1.job_date WHERE j1.job_date = @jobdate AND j1.item IN (SELECT j.item AS current_item FROM job AS j WHERE j.job_date = @jobdate AND j.item NOT LIKE 'Indirect' AND j.job NOT LIKE 'C%' ) GROUP BY c.item, c.qty_ordered, j1.whse, j1.job, j1.suffix ORDER BY j1.whse, c.item 未与任何else语句相关联。当你遇到一个不可接受的角色时,你不会打破你的循环。

您的整个功能应该被重写。尝试更像这样的东西:

if

答案 1 :(得分:1)

你似乎把Delphi与Python混淆了。 Python的for循环可能有else个子句,如果你不提前退出循环就会执行这些子句。德尔福没有这个功能。

有一些方法可以通过使用一些额外的变量和逻辑来模拟这种行为,但我认为这并不是你真正需要的。

如果您要检测26个不同的字母和空格,则验证字符串内容不是问题。 (有超过26个字母,但我认为你对此不感兴趣。)你的代码正确地检测到非字母,非空格字符并报告它们。

当您检测到错误时,您需要重新考虑函数的工作方式。现在,该函数打印一条错误消息(对于每个无效字符),但那么返回值应该是什么?在这种情况下,您不会分配结果。也许您打算让函数重新提示名称,直到用户输入有效值。如果这就是你想要的,那么当然你需要在循环中包含对ReadLn的调用;您当前的代码只能读取输入一次。