0

I have a set of nullable decimal properties that are decorated with a Range validation attribute with values -40.00 to +40.00 but when a user inputs a positive value with a leading positive(+) sign, the validation returns a false result displaying the error message. Yet a value of -1.50 will return a true validation result as it should.

The leading plus(+) sign cannot be removed as it is a business rule.

[Display(ResourceType = typeof(Translation), Name = "Profit_Q1")]
[Range(-40.00, +40.00)]
public decimal? profit_q1{ get; set; }

Any workarounds or am I doing something wrong? I have searched online and on stack and but cannot seem to find even a remotely similar answer.

  • It doesn't make sense to have a nullable decimal and also to check whether the value is in a certain range – spzvtbg Sep 14 '20 at 14:53
  • @spzvtbg you don't know how he's using this so I can't see how your comment is valid. – MikeJ Sep 14 '20 at 15:06
  • I doubt this matters but have you tried the overload of the Range attribute that takes the type and two string values? What you really want is to be able to specify the number styles with the positive sign but I can't see how to do that. – MikeJ Sep 14 '20 at 15:11
  • As a last resort you could always create a custom validator. https://stackoverflow.com/questions/3413715/how-to-create-custom-data-annotation-validators Hopefully there's a better way. – MikeJ Sep 14 '20 at 15:13
  • However both overloads from RangeAttribute working for me.` [Range(-40.00, 40.00, ErrorMessage = "Invalid amount!")] [Range(typeof(decimal), "-40", "40", ErrorMessage = "Invalid amount!")] public decimal? Amount { get; set; }` - I just tested them – spzvtbg Sep 14 '20 at 15:20
  • @MikeJ keeping the custom validator as a last resort. Hoping to find something that would work – Sushmit Saxena Sep 14 '20 at 15:27
  • @spzvtbg so it does not fail a value like "+1" as in input? Interesting if that's the case maybe it's just using the positive indicator for the Range attirbute. – MikeJ Sep 14 '20 at 15:40
  • @MikeJ No, it dosen't! But in OP's case with this overload all values will be compared als integer. – spzvtbg Sep 14 '20 at 16:22
  • 1
    @SushmitSaxena - Use the [Range(typeof(decimal), "-40,00", "+40,00", ErrorMessage = "Invalid amount!")] or [Range(typeof(decimal), "-40.00", "+40.00", ErrorMessage = "Invalid amount!", ConvertValueInInvariantCulture = true, ParseLimitsInInvariantCulture = true)]. And have in mind that the values are formatted by default whit decimal comma instead of point separator, which means that +40.00 for comparerer is equals 4000. You can improve your question where you write that -1.50 is valid maybe you mean -1.5 that for comparerer is -15 and is still greater than -40,00 – spzvtbg Sep 14 '20 at 16:46
  • Creating a new property of type string worked for me; decorated with the original Range validation attribute. Which I'm not sure why [Range(-40.00, +40.00)] public string profit_q1_string { get; set; } Thank you MikeJ and @spzvtbg for your time and efforts, much appreciated. – Sushmit Saxena Sep 15 '20 at 10:22

0 Answers0