Solved

# Decimal validation using c#

Posted on 2014-11-19
195 Views
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")

}
``````
//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")
}
``````
0
Question by:ksd123

LVL 39

Accepted Solution

Kyle Abrahams earned 250 total points
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 48

Expert Comment

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);
``````

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 74

Assisted Solution

käµfm³d   👽 earned 250 total points
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

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 74

Expert Comment

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.