0

I am developing an ASP.Net web API application and I want to validate users' number inputs for the culture. Which means, if user has entered 12.0 when he is has used the culture as "no" (Norwegian), I should block this request and send an error to user. How can I validate user number inputs considering the culture of the user. I was able to do this thing for users' Date inputs by using DateTime.ParseExact() method. Because this method gives an exception if the parsing number format is not in the specified culture format. But there is no method as Double.ParseExact() in C# api. How can I do this?

roryok
  • 9,325
  • 17
  • 71
  • 138
Thilok Gunawardena
  • 924
  • 5
  • 22
  • 44
  • I think you mean 'norwegian' – roryok Dec 18 '12 at 13:48
  • Yes of course. Sorry for the mistake. – Thilok Gunawardena Dec 18 '12 at 14:10
  • 1
    If it's meant to be a decimal value (stored as double), why not just replace `,` with `.` before converting? I believe that's the only variation that exists – roryok Dec 19 '12 at 09:40
  • You can use TryParse, and if that returns false (meaning it failed to parse) then you can throw an exception of your choice. – Display name Apr 28 '17 at 17:54
  • @roryok, For just the decimal point, that may be true. But if one also wants to handle grouping separators, then there are more variations. It simply depends on the goal. Here's a good link for more: https://msdn.microsoft.com/en-us/globalization/mt662324.aspx – Display name Apr 28 '17 at 17:59

0 Answers0