imsan
imsan

Reputation: 449

RegEx with fluent validation in C# - how to not allow spaces and certain special characters in a password?

This is the fluent validation for the password in my C# application so far

RuleFor(request => request.Password)
    .NotEmpty()
    .MinimumLength(8)
    .Matches("[A-Z]+").WithMessage("'{PropertyName}' must contain one or more capital letters.")
    .Matches("[a-z]+").WithMessage("'{PropertyName}' must contain one or more lowercase letters.")
    .Matches(@"(\d)+").WithMessage("'{PropertyName}' must contain one or more digits.")
    .Matches(@"[""!@$%^&*(){}:;<>,.?/+\-_=|'[\]~\\]").WithMessage("'{ PropertyName}' must contain one or more special characters.")
    .Matches("(?!.*[£# “”])").WithMessage("'{PropertyName}' must not contain the following characters £ # “” or spaces.")
    .Must(pass => !blacklistedWords.Any(word => pass.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0))
        .WithMessage("'{PropertyName}' contains a word that is not allowed.");

The following part currently doesn't work

.Matches("(?!.*[£# “”])").WithMessage("'{PropertyName}' must not contain the following characters £ # “” or spaces.")

For example when the password is 'Hello12!#' no validation errors are returned. £ # “” and spaces should not be allowed in a password and if any of these are present the validation should fail with the ''{PropertyName}' must not contain the following characters £ # “” or spaces.' error message.

How to amend this to make it work as it should?

Upvotes: 3

Views: 10507

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626932

You can use

RuleFor(request => request.Password)
    .NotEmpty()
    .MinimumLength(8)
    .Matches("[A-Z]").WithMessage("'{PropertyName}' must contain one or more capital letters.")
    .Matches("[a-z]").WithMessage("'{PropertyName}' must contain one or more lowercase letters.")
    .Matches(@"\d").WithMessage("'{PropertyName}' must contain one or more digits.")
    .Matches(@"[][""!@$%^&*(){}:;<>,.?/+_=|'~\\-]").WithMessage("'{ PropertyName}' must contain one or more special characters.")
    .Matches("^[^£# “”]*$").WithMessage("'{PropertyName}' must not contain the following characters £ # “” or spaces.")
    .Must(pass => !blacklistedWords.Any(word => pass.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0))
        .WithMessage("'{PropertyName}' contains a word that is not allowed.");

Note:

  • .Matches(@"[][""!@$%^&*(){}:;<>,.?/+_=|'~\\-]") - this matches an ASCII punctuation anywhere inside a string, and if not, the error pops up with the corresponding message
  • .Matches("^[^£# “”]*$") - this matches the whole string each char of which cannot be £, #, space, or . If any char is equal to at least one of these chars, the error message pops up.

Regarding [][""!@$%^&*(){}:;<>,.?/+_=|'~\\-], the ] is the first char in the character class and does not have to be escape. The - is placed at the end of the character class and does not have to be escaped either.

Upvotes: 5

Related Questions