How to validate a password (must contain uppercase, lowercase, digit, and special character) in Pascal

1 Answer

0 votes
program PasswordCheck;

function IsValidPassword(pass: string): boolean;
var
  i: integer;
  c: char;
  upper, lower, digit, special: boolean;
  specials: string;
begin
  upper := False;
  lower := False;
  digit := False;
  special := False;
  specials := '!@#$%^&*';

  for i := 1 to Length(pass) do
  begin
    c := pass[i];

    if c in ['A'..'Z'] then
      upper := True
    else if c in ['a'..'z'] then
      lower := True
    else if c in ['0'..'9'] then
      digit := True
    else if Pos(c, specials) > 0 then
      special := True;
  end;

  IsValidPassword := upper and lower and digit and special;
end;

var
  password1, password2: string;

begin
  password1 := 'aT5#op09!';
  if IsValidPassword(password1) then
    Writeln('Valid password')
  else
    Writeln('Invalid password');

  password2 := 'aT#opQ!';
  if IsValidPassword(password2) then
    Writeln('Valid password')
  else
    Writeln('Invalid password');
end.




(*
run:

Valid password
Invalid password

*)

 



answered Apr 25 by avibootz
...