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
Solved

Decimal validation using c#

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 41
Finding Events logs for IIS website that restarts 2 14
VB.NET Textbox input validation 4 40
VS 2017 18 49
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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