Skip to content

Client validation with RangeAttribute #59722

@McVanS

Description

@McVanS

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions