?
Solved

Decimal validation using c#

Posted on 2014-11-19
5
Medium Priority
?
237 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
[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
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 1000 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 52

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 1000 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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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