0

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?

Sid M
  • 4,354
  • 4
  • 30
  • 50
matk
  • 1,528
  • 2
  • 14
  • 25
  • 3
    `11.1` is valid since `.` is the group separator even if it's not the way you'd write the number `111`. Related: http://stackoverflow.com/a/26301899/284240 – Tim Schmelter Jan 21 '15 at 10:52
  • @TimSchmelter I'm aware of this behavior, but I'm trying to find a way around it. "11.1" simply is not a valid number in German, and my validation should reflect that. – matk Jan 21 '15 at 10:57
  • Technically it is valid, but if you want this behavior could you not detect the `.` character, then use a regular expression to test if they are in the correct positions? – Octopoid Jan 21 '15 at 11:00
  • @matk: so have you seen the answer of J. Skeet that i've linked above since he also provides a way to validate it. This may also be helpful: http://stackoverflow.com/questions/5927498/verify-input-string-is-valid-decimal-number – Tim Schmelter Jan 21 '15 at 11:02
  • @Octopoid I don't think it is that trivial, considering that in other cultures (which I also need to support) the `.` character is the decimal separator. As a last resort I could strip out the `NumberGroupSeparator` before validating, but to me this seems a bit like a hack. – matk Jan 21 '15 at 11:04
  • Yes, you'd need to use a different reg exp for different cultures, if you want to rigidly enforce the 3 character group guidelines. – Octopoid Jan 21 '15 at 11:05
  • @TimSchmelter I did, but I was hoping there was some better way than a "_very_ primitive" emulation. – matk Jan 21 '15 at 11:07
  • Jon Skeets solution absolutely covers it, but personally in this case, I'd - detect the culture, set the group separator, detect the group separator, if present parse the number, if parse passes, use a culture specific regexp to check for 3 digit group separation. – Octopoid Jan 21 '15 at 11:09
  • One thing to consider if you're accepting float/double values - do you consider things like 1.2e-3 to be a valid number? – Octopoid Jan 21 '15 at 11:10
  • @Octopoid no, in 99.9% of all cases I wouldn't consider exponential notation to be valid. – matk Jan 21 '15 at 11:12
  • 1
    As we're talking about input validation, you'll need to get that up to 100%. :P Either way, you can always add extra detection for things like e. – Octopoid Jan 21 '15 at 11:14

0 Answers0