client-side validation trips on DataAnnotation Range attribute
Asked Answered
Z

4

7

I have the following code in my Model class:

    [Range(1, 100)]
    public decimal Price { get; set; }

After recent upgrade (I assume) of jquery.validate to 1.11.0, I am getting an error even if I enter valid value. If I turn off client validation in web.config - works fine. All other attributes (StringLength, Required) work fine. Generated HTML is the following (line breaks added for clarity):

<input class="text-box single-line" data-val="true" 
data-val-number="The field Price must be a number." 
data-val-range="The field Price must be between 1 and 100." 
data-val-range-max="100" data-val-range-min="1" 
data-val-required="The Price field is required." id="Price" name="Price" 
type="text" value="" />

I am pretty sure it worked before... Can't think of anything but the bug in jquery.validate.

Zackaryzacks answered 15/2, 2013 at 6:32 Comment(3)
Which jquery version are you using?Autocatalysis
problem is about jquery not validate check hereAutocatalysis
looks like it is a different problem; not related to jquery itself. Somewhere between jquery-validate and unobtrusive validationZackaryzacks
Z
1

Microsoft issued an update to microsoft.jQuery.Unobtrusive.Ajax and to microsoft.jQuery.Unobtrusive.Validation (from version '2.0.20710.0' to '2.0.30116.0') that fixes both .live and validation problems

Zackaryzacks answered 18/2, 2013 at 23:55 Comment(2)
I still have the problem with 2.0.30116.0. I updated everything.Famulus
@Rowan, you probably want to start a new thread and put more details... I'll be happy to help; but I don't have the problem any more with 2.0.30116 - so it's hard to troubleshoot further!Zackaryzacks
A
12

We are having the same problem with jQuery.validate 1.11.0 and Microsoft.jQuery.Unobtrusive.Validation 2.0.30116.0. Somewhere in the validation library updates, the number validator broke.

There is an open issue on the GitHub issue tracker relating to this problem: https://github.com/jzaefferer/jquery-validation/issues/626

Quoted in that issue:

return this.optional(element) || ( value >= param[0] && value <= param[1] );

Because this line checks strings, not numbers. If I have a range between 30 and 200, and I want to validate 120, then the string 120 is lesser then string 30.

This line must be something like this:

return this.optional(element) || ( Number(value) >= Number(param[0]) && Number(value) <= Number(param[1]) );

I have changed my copy of jquery.validate.js:

// http://docs.jquery.com/Plugins/Validation/Methods/range
range: function( value, element, param ) {
    return this.optional(element) || (value >= param[0] && value <= param[1]) || (Number(value) >= Number(param[0]) && Number(value) <= Number(param[1]));
},

Now the range operations work as intended using DataAnnotations:

[Range(1, 100)]
public decimal Price { get; set; }
Anderegg answered 20/2, 2013 at 20:52 Comment(3)
Looks like the actual adapter from Microsoft has the problem. The adapter must be adjusted to parse a Number (look for adapters.addMinMax in code) connect.microsoft.com/VisualStudio/feedback/details/776965/…Anderegg
+1 This workaround worked perfectly. Without it I was still having troubles as of 10 April 2013, even with the newest jQuery files.Famulus
The GitHub issue tracker in the link above reports the problem fixed in jQuery Validation V1.11.1 (published 25/03/2013). It now works for me. (I have Microsoft.jQuery.Unobtrusive.Validation 2.0.30506.0)Blackford
Z
1

Microsoft issued an update to microsoft.jQuery.Unobtrusive.Ajax and to microsoft.jQuery.Unobtrusive.Validation (from version '2.0.20710.0' to '2.0.30116.0') that fixes both .live and validation problems

Zackaryzacks answered 18/2, 2013 at 23:55 Comment(2)
I still have the problem with 2.0.30116.0. I updated everything.Famulus
@Rowan, you probably want to start a new thread and put more details... I'll be happy to help; but I don't have the problem any more with 2.0.30116 - so it's hard to troubleshoot further!Zackaryzacks
I
0

This happening because you have updated Jquery version and new version of Jquery has different new updates and they have also removed some function like .live and replaced it with .on

Go through it...

http://jquery.com/upgrade-guide/1.9/#live-removed

Interlink answered 15/2, 2013 at 6:47 Comment(1)
Thank you very much! Yes, I also had a popup about .live no longer supported after I upgraded jquery to 1.9.1. Once I added jquery.migrate, the popup went away. However validation error still remains! It may have the same root cause; but adding migrate doesn't help; and I didn't find anything in the list - github.com/jquery/jquery-migrate/blob/master/warnings.mdZackaryzacks
J
0

I run into this recently. I upgraded from MVC 2 to MVC 5. It seems that parameter name was changed from "minimum" and "maximum" to "min" and "max"

If you want to fix the issue without going through the hassle of upgrading your JavaScript files, search in your solution for these two lines and replace them with the better line

rule.ValidationParameters['minimum']

replace with

typeof(rule.ValidationParameters['minimum'])!='undefined'?rule.ValidationParameters['minimum']:rule.ValidationParameters['min']

and

rule.ValidationParameters['maximum'];

with

typeof(rule.ValidationParameters['maximum'])!='undefined'?rule.ValidationParameters['maximum']:rule.ValidationParameters['max']

Or alternatively, replace "RangeValidator" function with this code

Sys.Mvc.RangeValidator.create=function(rule){var $0=typeof(rule.ValidationParameters['minimum'])!='undefined'?rule.ValidationParameters['minimum']:rule.ValidationParameters['min'];var $1=typeof(rule.ValidationParameters['maximum'])!='undefined'?rule.ValidationParameters['maximum']:rule.ValidationParameters['max'];return Function.createDelegate(new Sys.Mvc.RangeValidator($0,$1),new Sys.Mvc.RangeValidator($0,$1).validate);}
Jeremiahjeremias answered 23/2, 2016 at 17:18 Comment(1)
Looks like you are extremely averse to the hassle of upgrading standard packages. Most developers are much more averse to make changes to standard packages, and in any event, upgraded from MVC 2 years ago!Zackaryzacks

© 2022 - 2024 — McMap. All rights reserved.