9

I have a property like so public decimal? MyProperty { get; set; }, it IS NOT required and IS nullable, but if I don't put a value > 0 the validator say that the field MyProperty must be a number, if I leave the field empty I receive the same error, and if I put a 0 (zero) I receive the same error.

Ex:
0 -> Error
1 -> Ok
0,00 -> Error
0,01 -> Ok
empty -> Error

I don't understand why this don't work, I'm using $.preferCulture("pt-BR"); but don't make sense, because the value 0,01 is accepted, than I don't believe that culture can be the problem.

PS: The validation don't work in client side, the server side work correctly.

0

1 Answer 1

10

Default number function has a bug. It test's parseFloat(value) == 0 and parseFloat for '0' returns 0.

To solve this, I have overwritten it as

$.validator.methods.number = function (value, element) {
  return parseFloat(value).toString() !== "NaN";
}

I know that isn't the best way, but this workaround solves my problem.

Sign up to request clarification or add additional context in comments.

1 Comment

I found this, and it was quite helpful, but it should probably also have noted that MVC doesn't include the validator code into your code for you, you have to do it yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.