Solved

Decimal validation using data annotations using c#

Posted on 2014-11-28
2
2,838 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

Industry Leaders: 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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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 video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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