?
Solved

Decimal validation using data annotations using c#

Posted on 2014-11-28
2
Medium Priority
?
3,058 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

801 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