dotnet / dotnet/aspnetcore

Client validation with RangeAttribute

Open
#59,722 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

Recently I revisited an old MVC concept and started updating some of the components to .Net 9. I started updating my JavaScript client validation code and ran into a problem with the RangeAttribute.

The new RangeAttribute now supports MinimumIsExclusive and MaximumIsExclusive parameters.

[Range(1,10)] renders HTML attributes
data-val="true" data-val-range="The field Test must be between 1 and 10." data-val-range-max="10" data-val-range-min="1"

[Range(1,10,MinimumIsExclusive = true, MaximumIsExclusive = false)] renders HTML attributes
data-val="true" data-val-range="The field Test must be between 1 exclusive and 10." data-val-range-max="10" data-val-range-min="1"

[Range(1,10,MinimumIsExclusive = false, MaximumIsExclusive = true)] renders HTML attributes
data-val="true" data-val-range="The field Test must be between 1 and 10 exclusive." data-val-range-max="10" data-val-range-min="1"

[Range(1,10,MinimumIsExclusive = false, MaximumIsExclusive = true)] renders HTML attributes
data-val="true" data-val-range="The field Test must be between 1 exclusive and 10 exclusive." data-val-range-max="10" data-val-range-min="1"

Note the default error messages correctly reflects the intension. The actual server sider validation works as expected.

Historically for client validation one would retrieve a data attribute from an input element to do client validation with. A data attribute indicating the exclusive state of the minimum and/or maximum value(s) is absent in the generated HTML element. Except for the error message there is no other way one can determine the exclusive state of the value

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.