Skip to content

String Rules ​

String rules validate textual content and text containment. They do not replace type validation; use Types and Files when the value must specifically be a string, then add these rules for content.

Alpha ​

Requires the value to contain only letters.

csharp
[Alpha]
csharp
RuleFor(x => x.FirstName)
    .Alpha();

AlphaNumeric ​

Requires the value to contain only letters and numbers.

csharp
[AlphaNumeric]
csharp
RuleFor(x => x.ReferenceCode)
    .AlphaNumeric();

AlphaDash ​

Requires the value to contain only letters, numbers, dashes, and underscores. Use it for slugs, handles, and similar identifier-style text.

csharp
[AlphaDash]
csharp
RuleFor(x => x.Slug)
    .AlphaDash();

Ascii ​

Requires the value to contain only single-byte ASCII characters.

csharp
[Ascii]
csharp
RuleFor(x => x.LegacyCode)
    .Ascii();

Lowercase ​

Requires the text value to already be lowercase, and to contain at least one character that has a case. Digits and punctuation neither pass nor fail it on their own, so abc-1 passes while 123 does not.

csharp
[Lowercase]
csharp
RuleFor(x => x.Username)
    .Lowercase();

Uppercase ​

Requires the text value to already be uppercase, and to contain at least one character that has a case. Digits and punctuation neither pass nor fail it on their own, so ABC-1 passes while 123 does not.

csharp
[Uppercase]
csharp
RuleFor(x => x.CountryCode)
    .Uppercase();

StartsWith ​

Requires the text value to start with one of the provided prefixes.

csharp
[StartsWith(params string[] values)]

[StartsWith("ORD-", "INV-")]
csharp
RuleFor(x => x.OrderNumber)
    .StartsWith("ORD-", "INV-");

EndsWith ​

Requires the text value to end with one of the provided suffixes.

csharp
[EndsWith(params string[] values)]

[EndsWith(".jpg", ".png")]
csharp
RuleFor(x => x.FileName)
    .EndsWith(".jpg", ".png");

Contains ​

Requires the text value or collection to contain one of the provided values.

csharp
[Contains(params string[] values)]

[Contains("admin", "editor")]
csharp
RuleFor(x => x.Roles)
    .Contains("admin", "editor");

DoesNotStartWith ​

Rejects text that starts with one of the provided prefixes.

csharp
[DoesNotStartWith(params string[] values)]

[DoesNotStartWith("admin", "system")]
csharp
RuleFor(x => x.Username)
    .DoesNotStartWith("admin", "system");

DoesNotEndWith ​

Rejects text that ends with one of the provided suffixes.

csharp
[DoesNotEndWith(params string[] values)]

[DoesNotEndWith(".tmp", ".bak")]
csharp
RuleFor(x => x.FileName)
    .DoesNotEndWith(".tmp", ".bak");

DoesNotContain ​

Rejects text or collections containing one of the provided values.

csharp
[DoesNotContain(params string[] values)]

[DoesNotContain("password", "secret")]
csharp
RuleFor(x => x.DisplayName)
    .DoesNotContain("password", "secret");

All packages are released under the MIT License.