Blog.

Password validation in C#

Alina
Alina

This is a method for password validation in C#. The password should meet following requirements

  • At least one lower case letter
  • At least one upper case letter
  • At least special character
  • At least one number
  • At least 8 characters
  • Can not be more than 32 characters
  • Can not contain space


public static Result MatchPasswordMinimumCriteria(this CreateUser user)
    {
        if (user.Password.IsShorterThen(8))
        {
            return Result.Failure("Password cannot be less then 8 characters");
        }
        
        if (user.Password.IsLongerThen(32))
        {
            return Result.Failure("Password cannot be more then 32 characters");
        }

        if (user.Password.Contains(" "))
        {
            return Result.Failure("Password cannot contain space");
        }

        bool uppercaseCharacter = false;
        bool lowerCaseCharacter = false;
        bool number = false;
        bool specialCharacter = false;

        string specialCharacterString = @"%!@#$%^&*()?/>.<,:;'\|}]{[_~`+=-" + "\"";
        HashSet specialCharacters =  specialCharacterString.ToCharArray().ToHashSet();
        
        foreach (var character in user.Password)
        {
            if (uppercaseCharacter && lowerCaseCharacter && number &&    specialCharacter)
            {
                break;
            }

            if (number == false && character.IsInteger())
            {
                number = true;
                continue;
            }

            if (lowerCaseCharacter == false && character.IsLower())
            {
                lowerCaseCharacter = true;
                continue;
            }
            
            if (uppercaseCharacter == false && character.IsUpper())
            {
                uppercaseCharacter = true;
                continue;
            }

            if (specialCharacter == false && specialCharacters.Contains(character))
            {
                specialCharacter = true;
            }
        }

        if (lowerCaseCharacter == false)
        {
            return Result.Failure("Password must contain lowercase character");
        }
        
        if (uppercaseCharacter == false)
        {
            return Result.Failure("Password must contain uppercase character");
        }
        
        if (number == false)
        {
            return Result.Failure("Password must contain number");
        }
        
        if (specialCharacter == false)
        {
            return Result.Failure("Password must contain special character");
        }
        
        return Result.Success();
    }  

For better readability the method above can be split into multiple methods.


{
    public static bool IsLongerThen(this string text, int minimumLength)
    {
        return minimumLength < text.Length;
    }

    public static bool IsLongerOrEqualThen(this string text, int minimumLength)
    {
        return minimumLength <= text.Length;
    }

    public static bool IsShorterThen(this string text, int maximumLength)
    {
        return maximumLength > text.Length;
    }

    public static bool IsShorterOrEqualThen(this string text, int maximumLength)
    {
        return maximumLength >= text.Length;
    }

    public static bool IsEmpty(this string? text)
    {
        return text == "";
    }

    public static bool IsNotEmpty(this string? text)
    {
        return text != "";
    }

    public static bool IsNullOrEmpty(this string? text)
    {
        return string.IsNullOrEmpty(text);
    }

    public static bool IsNotNullOrEmpty(this string? text)
    {
        return !string.IsNullOrEmpty(text);
    }
}