I'm having some trouble validating a decimal value that is passed in from an HTML form. My setup is:
CurrentCulture.Name = "de-DE" (German culture)
CurrentCulture.NumberFormat.NumberGroupSeparator = "." (default for German culture)
CurrentCulture.NumberFormat.NumberDecimalSeparator = "," (default for German culture)
Form value = "11.1"
I need to validate the form value and would expect validation to fail, because "11.1" is not a valid number in the German culture, where "." is the group separator, not the decimal separator.
However, both of the following statements succeed, and convert the Form value to (decimal) 111, which is incorrect.
decimal.TryParse("11.1", NumberStyles.Any, CultureInfo.CurrentCulture, out x)
Convert.ToDecimal("11.1", CultureInfo.CurrentCulture)
Disallowing the NumberGroupSeparator (decimal.Parse("11.1", NumberStyles.Number ^ NumberStyles.AllowThousands)) is not an option, because I need to enable form input containing the group separator.
I'm also trying to avoid writing a custom RegEx because of all the subtleties involved with globalization.
How can I solve this? Is there a stricter parse method, somewhat equivalent to the DateTime.TryParseExact method for dates?