Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validating a password with delphi code

Looking to implement an end user dialog that requires them to create their own password.

Must be 9 characters long. 1 char must be upper case, 1 must be lowercase, one must be a number, ['0'..'9'] and one must be from a set of 6 predefined ascii chars like so. ['!','#','%','&','*','@'].

Have this completed. and works. However, what I wanted to do to was provide visible verification using the onchange event to change the color of the edit box to green if all requirments where met or RED if not. Valdating for the 9 char length is easy enough however checking the 9 various chars to ensure there is at least 1 upper, 1 lower, 1 number and 1 of the predefined is proving a tad difficult. Can anyone help please? Thank you.

This is the code:

procedure TPasswordForm.edtPassword1Change(Sender: TObject);      
  begin
    if Length(edtPassword1.Text <> 9 then
       edtPassword1.Color := clRed
    else
       edtPassword1.Color := clLime;
  end;
like image 308
Jerry Mallett Avatar asked Dec 05 '25 03:12

Jerry Mallett


1 Answers

For fixed char sets function might be quite simple. Note that it does not accept non-Latin chars.

function IsPasswordCrazy(const s: AnsiString): Boolean;
const
  C_Upcase = 1;
  C_Locase = 2;
  C_Digit = 4;
  C_SpecSym = 8;
  C_All = C_Upcase or C_Locase or C_Digit or C_SpecSym;
var
  i, keys: integer;
begin

  if Length(s) <> 9 then begin
    Result := False;
    Exit;
  end;

  keys := 0;
  for i := 1 to Length(s) do
    case s[i] of
      'A'..'Z': keys := keys or C_Upcase;
      'a'..'z': keys := keys or C_Locase;
      '0'..'9': keys := keys or C_Digit;
      '!','#','%','&','*','@': keys := keys or C_SpecSym;
    end;

  Result := keys = C_All;
end;
like image 135
MBo Avatar answered Dec 07 '25 16:12

MBo