'Password validation REGEX to disallow whitespaces

  • Password cannot contain white spaces
  • must contain at least one numeric char
  • must contain 1 capital letter
  • and be at least 8 characters in length, max 15

this is what i've got, which does all except WHITE SPACE rule.

((?=.*\d)(?=.*[A-Z]).{8,15})

what to add for that?

Thanks a lot! Language: c#, asp:RegularExpressionValidator



Solution 1:[1]

^((?!.*[\s])(?=.*[A-Z])(?=.*\d).{8,15})

Solution 2:[2]

Just match:

^(?!.* )(?=.*\d)(?=.*[A-Z]).{8,15}$

How it works:

.{8,15} means: 8 to 15 characters

(?!.* ) means: does not contain " "
(?=.*\d) means: contains at least one digit.
(?=.*[A-Z]) means: contains at least one capital letter

Solution 3:[3]

As an alternative to RegEx, have you considered just basic string parsing? In other words, if you're needing assistance to write the RegEx, what will happen with the maintainability over time?

Simple string parsing is much easier to understand for most of us. Those that follow in our footsteps will have a much easier time understanding the code and adding other requirements as well.

Here's an example using string parsing that is self-documenting, even without the error messages.

/// <summary>
/// Determines whether a password is valid.
/// </summary>
/// <param name="password">The password.</param>
/// <returns>A Tuple where Item1 is a boolean (true == valid password; false otherwise).
/// And Item2 is the message validating the password.</returns>
public Tuple<bool, string> IsValidPassword( string password )
{
    if( password.Contains( " " ) )
    {
        return new Tuple<bool, string>( false, "Password cannot contain white spaces." );
    }

    if( !password.Any( char.IsNumber ) )
    {
        return new Tuple<bool, string>( false, "Password must contain at least one numeric char." );
    }

    // perhaps the requirements meant to be 1 or more capital letters?
    // if( !password.Any( char.IsUpper ) )
    if( password.Count( char.IsUpper ) != 1 )
    {
        return new Tuple<bool, string>( false, "Password must contain only 1 capital letter." );
    }

    if( password.Length < 8 )
    {
        return new Tuple<bool, string>( false, "Password is too short; must be at least 8 characters (15 max)." );
    }

    if( password.Length > 15 )
    {
        return new Tuple<bool, string>( false, "Password is too long; must be no more than 15 characters (8 min)." );
    }

    return new Tuple<bool, string>( true, "Password is valid." );
}

Solution 4:[4]

You can use

^(?=\D*\d)(?=[^A-Z]*[A-Z])\S{8,15}$
\A(?=\D*\d)(?=[^A-Z]*[A-Z])\S{8,15}\z

See the regex demo.

Details:

  • ^ / \A - start of string (note \A is not compatible with ECMAScript regex flavor)
  • (?=\D*\d) - at least one digit
  • (?=[^A-Z]*[A-Z]) - at least one uppercase ASCII letter
  • \S{8,15} - eight to fifteen non-whitespace chars
  • $ / \z - until end of string (note that \z is not compliant with the ECMAScript regex flavor (e.g. in JavaScript)).

Due to \S, there can be no whitespace in the string.

If you have a more complex pattern and you need to use a lookahead restriction, add a (?!\S*\s) lookahead:

^(?!\S*\s)(?=\D*\d)(?=[^A-Z]*[A-Z]).{8,15}$

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yevgen Borodkin
Solution 2
Solution 3 Metro Smurf
Solution 4 Wiktor Stribiżew