inno-setuppascalscript

How to check if a string is a number in Inno Setup


I'm using Inno Setup and want to check with Pascal Script if a string variable is an Integer (0-9 only, no hex). I have made this funcion:

function IsInt(s: string): boolean;
var
  i, len: Integer;
begin
  len := length(s);
    
  if len = 0 then
    result := false
  else
  begin
    result := true;
    for i := 1 to len do
    begin
      if not (s[i] in ['0'..'9']) then // !!! ERROR HERE !!!
      begin
        result := false;
        exit;
      end;
    end;
  end;
end; 

But the compiler raises an error:

Closing square bracket (']') expected.

How to fix it?

If I change the line to this:

  if not (s[i] in ['0','1','2','3','4','5','6','7','8','9']) then

It compiles but if the code is executed it gives this error:

Runtime Error - Invalid Type.

What to do?


Solution

  • Rather than using sets you could just do a simple range test, e.g.

    if (s[i] < '0') or (s[i] > '9') then
       ...