Format Check

A check is made that the characters conform to some defined pattern. For example, a national insurance number must have 2 letters followed by 6 digits followed by a single letter:

program FormatCheck;
{$APPTYPE CONSOLE}
uses
  SysUtils;
var
  strNI : string;

function ValidateNI (NI_Num : string) : Boolean;
var
  Error: Boolean;
  Count : integer;
begin
  Error := False;
  if length(NI_Num) = 9 then
    begin
      NI_Num := UpperCase(NI_Num);
      if not (NI_Num[1] in ['A' .. 'Z']) then
        Error := True;
      if not (NI_Num[2] in ['A' .. 'Z']) then 
        Error := True;
      for Count := 3 to 8 do
        begin
          if not (NI_Num[Count] in ['0' .. '9']) then
            Error := True;
        end;
      if not (NI_Num[9] in ['A' .. 'Z']) then
        Error := True;
    end
  else
    begin
      Error := True; //length check
    end;
  result := not Error;
end;

begin
  repeat
    writeln('Your NI number must have 2 letters followed' +
            'by 6 digits followed be one letter.');
    write('Please enter your NI number. ');
    readln(strNI);
    strNI := UpperCase(strNI);
  until ValidateNI(strNI);
  writeln('The  NI number ',strNI,' has been validated.');
  readln;
end.

Programming - a skill for life!

Checking data to see if it is sensible