4

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?

Sparky
  • 98,165
  • 25
  • 199
  • 285
Nadav
  • 357
  • 3
  • 10
  • Yes. [You should refer to this Github page for bugs](https://github.com/jzaefferer/jquery-validation/issues?state=open). SO is only for _your_ coding problems. https://github.com/jzaefferer/jquery-validation/issues?state=open – Sparky Mar 11 '13 at 16:07
  • Thanks, It's just that before I open a bug in JQuery I wanted to make sure I wasn't doing a stupid mistake... – Nadav Mar 11 '13 at 16:56
  • I appreciate that, but that's not really what SO is for. Please flag your question for closure or deletion. Thanks. – Sparky Mar 11 '13 at 17:01
  • What's the bug # you created for this? I'm seeing the same problem. – Josh Mouch Mar 12 '13 at 13:49
  • Same issue. I thought it was my code. I go to Stackoverflow for this sort of issue. Its a lot easier to find these things on stack than on peoples random project repo. Obviously if someone finds a link on the issue page it would be the accepted answer – GraemeMiller Mar 29 '13 at 12:19

2 Answers2

0

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; }
Community
  • 1
  • 1
jishnu saha
  • 175
  • 7
  • I don't have an Integer data anotation in ASP.MVC3. Where did you get it? Anyway, the problem is a bug in the JQuery validation code so I doubt it will help... Anyway, thanks for the suggestion.. – Nadav Mar 11 '13 at 17:01
0

[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.

jishnu saha
  • 175
  • 7