Search code examples
delphidelphi-7

How to verify if the first character in a string is letter or number?


Only account queries, complaints and suggestions with a valid account numbers will be processed, Write a subprogram called ValidateAccNum to receive an account number as a string indicating weather it is valid or not. A valid account number must satisfy the following criteria:

  • The account number must have only SEVEN characters.

  • The account number must start with a letter.

The problem I am having is figuring out if the first character of the account number is a string or integer. Heres my code:

procedure TfrmQuestion3.ValidateAccNum(AccNum: string);
var
  RealACCNum : Boolean;
  ACCNumLength : Integer;
  StartACCNum : string;
begin
  RealACCNum := False;
  ACCNumLength := Length(AccNum);
  StartACCNum := AccNum[1];

  If (ACCNumLength = 7) and (StartACCNum = string) // <--- This is obviously the 
                                                   //      problem, I know its wrong
    then RealACCNum = True
    else exit;
  end;

So how do I check weather its a String character or an Integer?


Solution

  • if (Length(AccNum) = 7) and IsCharAlpha(AccNum[1]) then
    

    You can check like that ^
                                          |