anand
anand

Reputation: 643

Need to find if the text box (edit box) contains only alphabet in inno setup

I have code in inno to check if a value of particular text box contains only alphabet, but the code is throwing compile error.

close block (']') expected

Below is my code.

if not DBPage.Values[0] in ['a'..'z', 'A'..'Z'] then
begin
MsgBox('You must enter alphabets only.', mbError, MB_OK);
end;

Where DBPage.Values[0] is the text box in my custom page.

Upvotes: 3

Views: 1126

Answers (1)

TLama
TLama

Reputation: 76733

First of all, InnoSetup scripting doesn't allow constant set ranges. Even though, your code wouldn't do what seems you want to. By using DBPage.Values[0] you're accessing the whole string value, not a single char as you probably wanted.

If you don't want to write a pretty complex condition for all alphabetical chars, you can advance from the Windows API function IsCharAlpha. The following code shows, how to use it in your code:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

var
  DBPage: TInputQueryWizardPage;

function IsCharAlpha(ch: Char): BOOL;
  external 'IsCharAlpha{#AW}@user32.dll stdcall';

function NextButtonClick(CurPageID: Integer): Boolean;
var
  S: string;  
  I: Integer;
begin
  Result := True;
  // store the edit value to a string variable
  S := DBPage.Values[0];
  // iterate the whole string char by char and check if the currently
  // iterated char is alphabetical; if not, don't allow the user exit
  // the page, show the error message and exit the function
  for I := 1 to Length(S) do
    if not IsCharAlpha(S[I]) then
    begin
      Result := False;
      MsgBox('You must enter alphabets only.', mbError, MB_OK);
      Exit;
    end;
end;

procedure InitializeWizard;
begin
  DBPage := CreateInputQueryPage(wpWelcome, 'Caption', 'Description', 'SubCaption');
  DBPage.Add('Name:', False);
  DBPage.Values[0] := 'Name';
end;

Out of curiosity, the following script prevents the edit to enter anything else but alphabetical chars:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

var
  DBPage: TInputQueryWizardPage;

function IsCharAlpha(ch: Char): BOOL;
  external 'IsCharAlpha{#AW}@user32.dll stdcall';

procedure AlphaEditKeyPress(Sender: TObject; var Key: Char);
begin
  if not IsCharAlpha(Key) and (Key <> #8) then
    Key := #0;
end;

procedure InitializeWizard;
var
  ItemIndex: Integer;
begin
  DBPage := CreateInputQueryPage(wpWelcome, 'Caption', 'Description', 'SubCaption');
  ItemIndex := DBPage.Add('Name:', False);
  DBPage.Values[ItemIndex] := 'Name';
  DBPage.Edits[ItemIndex].OnKeyPress := @AlphaEditKeyPress;
end;

Upvotes: 5

Related Questions