Checking for Whitespace in FluentValidation
Asked Answered
S

5

9

I'm using FluentValidation and trying to create a rule that throws error if there is any whitespace in the string, i.e. for a username.

I've reviewed these SOs, but doesn't seem to work, I'm sure my syntax is off just a little?

What is the Regular Expression For "Not Whitespace and Not a hyphen" and What is the Regular Expression For "Not Whitespace and Not a hyphen"

RuleFor(m => m.UserName).NotEmpty().Length(3, 15).Matches(@"/^\S\z/");

or

RuleFor(m => m.UserName).NotEmpty().Length(3, 15).Matches(@"[^\s]");

Neither of these seem to work. Other rules are not empty and between 3 and 15 characters.

Steck answered 11/3, 2014 at 21:34 Comment(0)
G
3

Just modifying your original rule a bit
edit Ok, removing delimiters as suggested.

RuleFor(m => m.UserName).NotEmpty().Length(3, 15).Matches(@"\A\S+\z");

All it does is force there to be non-whitespace in the whole string from start to finish.

Alternatively, I guess you could combine them into 1 match as in

RuleFor(m => m.UserName).Matches(@"\A\S{3,15}\z");
Galah answered 11/3, 2014 at 22:10 Comment(5)
This works, except for the / and \ surrounding the expression. Remove those and either works. If you want to update the answer, I can mark it complete. Thanks! RuleFor(m => m.UserName).NotEmpty().Matches(@"\A\S{3,15}\z");Steck
RuleFor(m => m.UserName).NotEmpty().Matches(@"\A\S{3,15}\z"); not worked for me. It is showing validating even also when i am entering any charactersVirology
@AnmolRathod - RuleFor(m => m.UserName).NotEmpty().Matches(@"\A\S{3,15}\z"); not worked for me. It is showing validating even also when i am entering any characters This -> \A is absolute beginning of the string. \z is absolute end of string \S{3,15} is 3 to 15 any character except whitespace. And there you have it ..Galah
@sln what would be final regex than ?Virology
@AnmolRathod - The final regex was posted March 14, 2014.Galah
C
3

Try the char.IsWhiteSpace

RuleFor(m => m.UserName).NotEmpty().Length(3, 15).Must(userName => !userName.All(c => char.IsWhiteSpace(c)))
Carbonado answered 11/3, 2014 at 21:39 Comment(1)
Was not able to get this to work. Also might there be a performance if using LINQ to check every character vs. the Regex approach.Steck
G
3

Just modifying your original rule a bit
edit Ok, removing delimiters as suggested.

RuleFor(m => m.UserName).NotEmpty().Length(3, 15).Matches(@"\A\S+\z");

All it does is force there to be non-whitespace in the whole string from start to finish.

Alternatively, I guess you could combine them into 1 match as in

RuleFor(m => m.UserName).Matches(@"\A\S{3,15}\z");
Galah answered 11/3, 2014 at 22:10 Comment(5)
This works, except for the / and \ surrounding the expression. Remove those and either works. If you want to update the answer, I can mark it complete. Thanks! RuleFor(m => m.UserName).NotEmpty().Matches(@"\A\S{3,15}\z");Steck
RuleFor(m => m.UserName).NotEmpty().Matches(@"\A\S{3,15}\z"); not worked for me. It is showing validating even also when i am entering any charactersVirology
@AnmolRathod - RuleFor(m => m.UserName).NotEmpty().Matches(@"\A\S{3,15}\z"); not worked for me. It is showing validating even also when i am entering any characters This -> \A is absolute beginning of the string. \z is absolute end of string \S{3,15} is 3 to 15 any character except whitespace. And there you have it ..Galah
@sln what would be final regex than ?Virology
@AnmolRathod - The final regex was posted March 14, 2014.Galah
P
3

This worked for me with FluentValidation.MVC5 6.4.0

RuleFor(x => x.username).Must(u => !u.Any(x => Char.IsWhiteSpace(x)));
Prothorax answered 4/5, 2017 at 15:11 Comment(0)
P
2

Try this:

RuleFor(m => m.UserName).NotEmpty().Length(3, 15).Must (u => !string.IsNullOrWhiteSpace(u));
Panpsychist answered 11/3, 2014 at 21:43 Comment(1)
That doesn't work. I'm still able to put whitespace.Prothorax
S
0
 public static string RemoveWhitespace(this string input)
 {
    return new string(input.ToCharArray()
        .Where(c => !Char.IsWhiteSpace(c))
        .ToArray());
 }

RuleFor(x => x.Username).Must(s => s.Length == s.RemoveWhitespace().Length);
Spoofery answered 8/11, 2023 at 11:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.