Character Check

Characters must conform to one or more rules. The program SurnameCheck rejects input strings containing characters other than letters of our alphabet, the hyphen and the apostrophe. This program checks whether each input character is in a set of allowed characters.

program SurnameCheck;
{$APPTYPE CONSOLE}
uses
  SysUtils;
var
  strSurname : string;

function ValidateSurname (LastName : string) : Boolean;
var
  Error: Boolean;
  Count : integer;
begin
  Error := False;
  for Count := 1 to length(LastName) do
    begin
      if not (LastName[Count] in ['A'..'Z', 'a'..'z', '-', chr(39)]) then
        Error := True;
    end;
  result := not Error;
end;

begin
  repeat
    write('Please enter your surname. ');
    readln(strSurname);
  until ValidateSurname(strSurname);
  writeln('The surname ', strSurname,' has been validated.');
  readln;
end.

Programming - a skill for life!

Checking data to see if it is sensible