Solved

Decimal validation using data annotations using c#

Posted on 2014-11-28
2
2,697 Views
Last Modified: 2014-11-29
Hi Experts,

I am using data annotations to validate Model properties using MVC5 and need to validate decimal property which is both Required and Range which must be between 2 and 100.

If I don't enter any data for Price field ie. 0 (since it is decimal) the Required validation message should show.But in  both the cases(Required and Range)  Range validation message is showing.Can anyone guide me what went wrong in the below code snippet?

        [Required(ErrorMessage = "The Price is required")]
        [Range(2.00, 100.00, ErrorMessage = "The  Price must be between 2 and 100")]
        public decimal Price { get; set; }

Open in new window

0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 40470817
Looks to me like the Required attribute is working as designed. It's checking for an empty string or null input. The zero is a valid input and will now check for a range.

If you want zero to be invalid, I think you can implement your own class and override the method that does the validation. I have **NOT** tested this, but I think that this technique is the way to go if you can at least try doing this.

Start with creating a class based on the Required Class:

 
  public class DecimalRequired : System.ComponentModel.DataAnnotations.RequiredAttribute
    {
        public override bool IsValid(object value)
        {
            decimal d = decimal.MinValue;
            if (decimal.TryParse(value.ToString(), out d))
            {
                if (d == 0.0M)
                {
                    return false;
                }
            }
            return base.IsValid(value);
        }
    }

Open in new window


Then, your code would do like this:

        [DecimalRequired (ErrorMessage = "The Price is required")]
        [Range(2.00, 100.00, ErrorMessage = "The  Price must be between 2 and 100")]
        public decimal Price { get; set; }

Open in new window


That should get you to where you want to go.
0
 

Author Closing Comment

by:ksd123
ID: 40472180
Thank you so much.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question