使用 MVC 和数据注释在客户端添加大于 0 的验证器的最佳方法是什么?

发布于 2024-12-04 08:13:27 字数 118 浏览 1 评论 0原文

我希望能够仅在某个字段中的值大于 0 时才允许提交表单。我想也许 Mvc Range 属性将允许我仅输入 1 个值来表示仅大于测试,但是那里没有运气,因为它坚持最小值和最大值。

有什么想法可以实现这一点吗?

I'd like to be able to only allow a form to submit if the value in a certain field is greater than 0. I thought maybe the Mvc Range attribute would allow me to enter only 1 value to signify only a greater than test, but no luck there as it insists on Minimum AND Maximum values.

Any ideas how this can be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋心╮凉 2024-12-11 08:13:27

您无法存储大于基础数据类型可以容纳的数字,因此 Range 属性需要最大值这一事实是一件非常好的事情。请记住,现实世界中不存在 ,因此以下内容应该有效:

[Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")]
public int Value { get; set; }

You can't store a number bigger than what your underlying data type could hold so that fact that the Range attribute requires a max value is a very good thing. Remember that doesn't exist in the real world, so the following should work:

[Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")]
public int Value { get; set; }
枕梦 2024-12-11 08:13:27

我发现这个答案旨在验证浮点数/双精度数的任何正值。事实证明,这些类型有一个有用的常量“Epsilon”

表示大于零的最小正 System.Double 值。

    [Required]
    [Range(double.Epsilon, double.MaxValue)]
    public double Length { get; set; }

I found this answer looking to validate any positive value for a float/double. It turns out these types have a useful constant for 'Epsilon'

Represents the smallest positive System.Double value that is greater than zero.

    [Required]
    [Range(double.Epsilon, double.MaxValue)]
    public double Length { get; set; }
八巷 2024-12-11 08:13:27

您可以像这样创建自己的验证器:

    public class RequiredGreaterThanZero : ValidationAttribute
{
    /// <summary>
    /// Designed for dropdowns to ensure that a selection is valid and not the dummy "SELECT" entry
    /// </summary>
    /// <param name="value">The integer value of the selection</param>
    /// <returns>True if value is greater than zero</returns>
    public override bool IsValid(object value)
    {
        // return true if value is a non-null number > 0, otherwise return false
        int i;
        return value != null && int.TryParse(value.ToString(), out i) && i > 0;
    }
}

然后在模型中“包含”该文件并将其用作属性,如下所示:

    [RequiredGreaterThanZero]
    [DisplayName("Driver")]
    public int DriverID { get; set; }

我通常在下拉验证中使用它。请注意,因为它扩展了validationattribute,所以您可以使用参数自定义错误消息。

You can create your own validator like this:

    public class RequiredGreaterThanZero : ValidationAttribute
{
    /// <summary>
    /// Designed for dropdowns to ensure that a selection is valid and not the dummy "SELECT" entry
    /// </summary>
    /// <param name="value">The integer value of the selection</param>
    /// <returns>True if value is greater than zero</returns>
    public override bool IsValid(object value)
    {
        // return true if value is a non-null number > 0, otherwise return false
        int i;
        return value != null && int.TryParse(value.ToString(), out i) && i > 0;
    }
}

Then "include" that file in your model and use it as an attribute like this:

    [RequiredGreaterThanZero]
    [DisplayName("Driver")]
    public int DriverID { get; set; }

I commonly use this on dropdown validation. Note that because it's extending validationattribute you can customize the error message with a parameter.

夜还是长夜 2024-12-11 08:13:27

上面的验证器适用于整数。我将其扩展为使用小数:

    public class RequiredDecimalGreaterThanZero : ValidationAttribute
    {
        /// <summary>
        /// Designed for dropdowns to ensure that a selection is valid and not the dummy "SELECT" entry
        /// </summary>
        /// <param name="value">The integer value of the selection</param>
        /// <returns>True if value is greater than zero</returns>
        public override bool IsValid(object value)
        {
            // return true if value is a non-null number > 0, otherwise return false
            decimal i;
            return value != null && decimal.TryParse(value.ToString(), out i) && i > 0;
        }
    }

The above validator works with integers. I extended this to work with a decimal:

    public class RequiredDecimalGreaterThanZero : ValidationAttribute
    {
        /// <summary>
        /// Designed for dropdowns to ensure that a selection is valid and not the dummy "SELECT" entry
        /// </summary>
        /// <param name="value">The integer value of the selection</param>
        /// <returns>True if value is greater than zero</returns>
        public override bool IsValid(object value)
        {
            // return true if value is a non-null number > 0, otherwise return false
            decimal i;
            return value != null && decimal.TryParse(value.ToString(), out i) && i > 0;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文