Jquery validation does not validate numbers properly?

*爱你&永不变心* 提交于 2019-12-12 13:23:18

问题


I've got a problem with validation of numbers using jquery unobtrusive validation. The versions I'm using are: ASP.MVC 3. JQuery 1.9.1 JQuery UI 1.10.1 JQuery Validation 1.11.0

the input I'm trying to validate is :

<input type="number" value="0" name="Data.time_between_loops.Planned.hour" id="Data_time_between_loops_Planned_hour" data-val-required="The hour field is required." data-val-range-min="0" data-val-range-max="23" data-val-range="The field hour must be between 0 and 23." data-val-number="The field hour must be a number." data-val="true" class="hour valid">

The validation is performed, but the problem is that it is performed on the value AS a string, i.e. when I type 4 I get a validation error.

I looked at the code of jquery.validation and it looks like it does not convert the any of the values (element value,min/max) to a number, and so the range function compares the values are strings. I assume that if you set the min/max range in jquery then the values will not be strings, but when you specify them in data attributes they ARE strings and jquery does not seem to handle this.

Is this ready a bug in jquery validation or am I doing something wrong?

Thanks, Nadav How do I fix this problem?


回答1:


Make the property data type integer in model.

follow these post :

Validation on int data type asp .net mvc3

[Integer(ErrorMessage="Are you stupid? Just fill in numbers only!")]
public int? TestValue { get; set; }



回答2:


[DataType(Integer)] is valid in MVC 3. you can do a trick. i.e. make the textbox type='text' and validate it with javascript that the box accept only numeric values. then you can validate the control by range validation.



来源:https://stackoverflow.com/questions/15341564/jquery-validation-does-not-validate-numbers-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!