Solved

Decimal validation using c#

Posted on 2014-11-19
5
211 Views
Last Modified: 2014-11-25
Hi Experts,

I am doing Model validation in web api and have to validate decimal field using C#.

Valid inputs: 3.00,38, 498, 4444.4
Invalid inputs: @@,##,3.#d

Here is the code
 public class Foo
    {
       
      [Required(ErrorMessage = "Price is required")]
        
        public decimal Price { get; set; }
       
    }

if(this.Price>=777.77m)
{

return new ValidationResult("Price can't be greater than 777.77")

}

Open in new window

//Want to check if input is decimal or not .How can I achieve using c# .Do we have any built-in function in c#?

else if (!input is decimal )
{
return new ValidationResult("Invalid Price fomat")
}

Open in new window

0
Comment
Question by:ksd123
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 40453235
Luckily it's done for you:

Decimal d;

if (!decimal.TryParse(input, out d))
  return new ValidationResult("Invalid Price fomat");
//else
   // do something?
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40453242
Hi,

Maybe Decimal.TryParse
string value;
decimal number;

// Parse a floating-point value with a thousands separator. 
value = "1,643.57";
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);  

Open in new window


Refer to http://msdn.microsoft.com/en-us/library/9zbda557(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Regards
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 40453278
If your model property is of type decimal, then you should not have to validate anything--if the submitted value cannot be converted to a decimal, then it will get put in as the default value. Unfortunately, since you are using decimal, the default value is zero, so you may want to change the type of that property. Using a nullable decimal (decimal?) would yield a default value of null. However, then you won't know if the value was incorrect or simply not submitted. The remaining choice would be to type the property as string, at which point you could do checks for null (value not submitted) or TryParse checks (ill-formatted value submitted).
0
 

Author Comment

by:ksd123
ID: 40454089
Hi Kyle,

I tried below code as you suggested,but giving error "invalid arguements".

decimal.TryParse takes string and decimal types as agruments.

Decimal d;

if (!decimal.TryParse(this.Price, out d))
  return new ValidationResult("Invalid Price fomat");
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40454217
TryParse is for attempting to turn strings into decimals, not decimals into decimals. This is why you get the error. Please see my previous comment.
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

Suggested Solutions

Title # Comments Views Activity
how to remove duplicate code from my project 5 50
Regarding swagger API 1 38
itextsharp with c# 3 18
Build a string of emails from a gridview 2 20
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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