Skip to content

Number Rules ​

Number rules validate numeric values, comparable sizes, digit counts, and numeric relationships. Comparable size rules adapt to the value type: strings use character count, arrays use item count, files use kilobytes, and numeric values use the number itself.

Numeric ​

Requires the value to be numeric.

csharp
[Numeric]
csharp
RuleFor(x => x.Amount)
    .Numeric();

Integer ​

Requires the value to be an integer.

csharp
[Integer]
csharp
RuleFor(x => x.Quantity)
    .Integer();

Min ​

Requires the value to be at least the provided number. For strings, collections, and files, the number is interpreted as length, count, or kilobytes.

csharp
[Min(double value)]

[Min(3)]
csharp
RuleFor(x => x.DisplayName)
    .Min(3);

Max ​

Requires the value to be no greater than the provided number. For strings, collections, and files, the number is interpreted as length, count, or kilobytes.

csharp
[Max(double value)]

[Max(255)]
csharp
RuleFor(x => x.DisplayName)
    .Max(255);

Between ​

Requires the value to be between the inclusive minimum and maximum.

csharp
[Between(double min, double max)]

[Between(1, 365)]
csharp
RuleFor(x => x.DurationDays)
    .Between(1, 365);

Size ​

Requires the value to match the exact numeric value, string length, collection count, or file size.

csharp
[Size(double value)]

[Size(6)]
csharp
RuleFor(x => x.Code)
    .Size(6);

Decimal ​

Requires the numeric value to have the configured number of decimal places.

csharp
[Decimal(int places)]

[Decimal(2)]
csharp
RuleFor(x => x.Price)
    .Decimal(2);

Digits ​

Requires the value to contain exactly the provided number of digits, counting the digits as written so a leading zero counts. Only a run of ASCII digits qualifies: a sign, a decimal point, or a thousands separator fails the rule rather than being skipped over.

csharp
[Digits(int digits)]

[Digits(8)]
csharp
RuleFor(x => x.Pin)
    .Digits(8);

DigitsBetween ​

Requires the digit count to be between the inclusive minimum and maximum.

csharp
[DigitsBetween(int min, int max)]

[DigitsBetween(8, 12)]
csharp
RuleFor(x => x.AccountNumber)
    .DigitsBetween(8, 12);

MinDigits ​

Requires the value to contain at least the provided number of digits.

csharp
[MinDigits(int min)]

[MinDigits(8)]
csharp
RuleFor(x => x.AccountNumber)
    .MinDigits(8);

MaxDigits ​

Allows the value to contain at most the provided number of digits.

csharp
[MaxDigits(int max)]

[MaxDigits(12)]
csharp
RuleFor(x => x.AccountNumber)
    .MaxDigits(12);

MultipleOf ​

Requires the numeric value to be a multiple of the provided number.

csharp
[MultipleOf(double value)]

[MultipleOf(5)]
csharp
RuleFor(x => x.Quantity)
    .MultipleOf(5);

GreaterThan ​

Requires the value to be greater than the provided number.

csharp
[GreaterThan(double value)]

[GreaterThan(0)]
csharp
RuleFor(x => x.Price)
    .GreaterThan(0);

GreaterThanOrEqual ​

Requires the value to be greater than or equal to the provided number.

csharp
[GreaterThanOrEqual(double value)]

[GreaterThanOrEqual(18)]
csharp
RuleFor(x => x.Age)
    .GreaterThanOrEqual(18);

LessThan ​

Requires the value to be less than the provided number.

csharp
[LessThan(double value)]

[LessThan(100)]
csharp
RuleFor(x => x.DiscountPercentage)
    .LessThan(100);

LessThanOrEqual ​

Requires the value to be less than or equal to the provided number.

csharp
[LessThanOrEqual(double value)]

[LessThanOrEqual(100)]
csharp
RuleFor(x => x.DiscountPercentage)
    .LessThanOrEqual(100);

All packages are released under the MIT License.