Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

client-side validation trips on DataAnnotation Range attribute

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.

like image 842
Felix Avatar asked Feb 15 '13 06:02

Felix


People also ask

How do you validate model data using DataAnnotations attributes?

To add validation using DataAnnotations attributesAdd a namespace declaration to the partial class that matches the namespace of the data model that you are using. An incorrect namespace used in a partial class results in a naked partial class, which is a partial class that is not associated with any other class.

What is range attribute?

The range attribute allows a numeric range to be specified for a slot when a numeric value is used in that slot. If a numeric value is not used in that slot, then no checking is performed.

Is data annotation server side validation in MVC?

In ASP.NET MVC application we can do the Server Side Validation on the Model using Data Annotations. Data Annotations is a namespace that provides attribute classes, and these classes define metadata for controls. In MVC we decorate Model Properties with these attribute classes to provide metadata.


1 Answers

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; }
like image 112
Derek Gates Avatar answered Nov 13 '22 05:11

Derek Gates