What is the best way of adding a greater than 0 validator on the client-side using MVC and data annotation?

asp.net MvcUnobtrusive Validation

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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; }

Solution 2 - asp.net Mvc

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; }

Solution 3 - asp.net Mvc

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.

Solution 4 - asp.net Mvc

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;
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjaffaView Question on Stackoverflow
Solution 1 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 2 - asp.net MvcPhilView Answer on Stackoverflow
Solution 3 - asp.net MvcJohn LordView Answer on Stackoverflow
Solution 4 - asp.net MvcGreg GumView Answer on Stackoverflow