Solved

Decimal validation using data annotations using c#

Posted on 2014-11-28
2
2,192 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
2 Comments
 
LVL 19

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now