Solved

Decimal validation using c#

Posted on 2014-11-19
5
195 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 39

Accepted Solution

by:
Kyle Abrahams earned 250 total points
Comment Utility
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

by:Rgonzo1971
Comment Utility
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 74

Assisted Solution

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

by:käµfm³d 👽
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.

771 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

11 Experts available now in Live!

Get 1:1 Help Now