开发者

Decimal is not validated correctly in MVC 3 with jQuery Validator

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 belie开发者_JS百科ve that culture can be the problem.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜