0
[Validator(typeof(foo))]
public class foo
{
    public double? bar { get; set; }
}

public class fooValidator : AbstractValidator<foo>
{
    public fooValidator()
    {
        RuleFor(x => x.bar)
            .NotEmpty()
            .GreaterThan(0d);
    }
}

I then display the input field for bar using the following:

@Html.EditorFor(model => model.Bar, new {  @class = "form-control", htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Bar)

However this presents some (to me) inexplicable behavior.

If I enter a number with a comma, e.g.: 1,1 it'll display an error message. However if the number has three, and only three (e.g. 19,800), numbers after the comma it'll gladly accept it as a valid input.

Why does this happen?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
muchotaco
  • 31
  • 5
  • Probably because it considers the `,` as thousand delimiter, so 19,800 will be considered as 19800 – Rafalon Aug 23 '18 at 14:11
  • 6
    Because that's a valid format for the culture your app is running under. – DavidG Aug 23 '18 at 14:12
  • What culture are you using? Some cultures use `,` as a thousands separator, including the default one, but are you expecting `.` or something else? – Joe Sewell Aug 23 '18 at 14:13
  • That makes a lot of sense. However, if I input `19800` the program works, but `19,800` makes it fail. – muchotaco Aug 23 '18 at 14:21
  • I'm confused. You say it's accepting it, but you're also say it "makes it fail". Can you more clearly identify what "it" is from that second statement, and if the "fail" is an error, tell us what the error is. By the time it's in a `double?`, the fact that it came from a string with a comma should be irrelevant. – Damien_The_Unbeliever Aug 23 '18 at 14:36
  • The Validator is accepting it (i.e. doesn't display an error message) but the program breaks further down the line if the number has a `,`. – muchotaco Aug 23 '18 at 14:47
  • Telling us it "breaks" or "fails" doesn't help *us* to help *you*. Please try to be more *specific* (if you're actually wanting to unbreak it) – Damien_The_Unbeliever Aug 23 '18 at 14:50
  • If I submit `19800` the form posts correctly to the Action in the Controller, however if I submit `19,800` I never hit the breakpoint I set in the Action. – muchotaco Aug 23 '18 at 15:12
  • The format accepted by default model binder depends to current culture set in web.config or machine.config. By default it uses `CultureInfo.InvariantCulture` which uses decimal point & comma thousand separator. – Tetsuya Yamamoto Aug 23 '18 at 15:15
  • Is this the option you're referring to `` ? – muchotaco Aug 23 '18 at 15:36

1 Answers1

0

By default MVC uses dot as separator, if you want to use with comma I suggest reading the following question: Accept comma and dot as decimal separator

If you want to improve your validation I suggest readind this another question: Int or Number DataType for DataAnnotation validation attribute

  • 1
    Your first sentence is not right, it's not an MVC default, it's entirely dependent on the culture of the system. – DavidG Aug 23 '18 at 14:26