List of all unobtrusive validation attributes for each Validation Attribute
Asked Answered
D

2

6

I need a reference list of all unobtrusive validation attributes for each Validation Attribute. Something like:

enter image description here

Delaunay answered 2/9, 2015 at 15:0 Comment(3)
Not clear what you are asking for here. Do you want a list of data annotations available as Attributes in MVC, a list of jquery validation validators, or what?Husk
I have updated the question :)Delaunay
Are you asking for us to recommend an off site resource?Dett
I
5

MVC ships with unobtrusive validators for each of the Data Annotation Validators that it provides. Taken from Validation with Data Annotation Validators, here is that list:

Using the Data Annotation Validator Attributes

When you use the Data Annotations Model Binder, you use validator attributes to perform validation. The System.ComponentModel.DataAnnotations namespace includes the following validator attributes:

  • Range – Enables you to validate whether the value of a property falls between a specified range of values.
  • ReqularExpression – Enables you to validate whether the value of a property matches a specified regular expression pattern.
  • Required – Enables you to mark a property as required.
  • StringLength – Enables you to specify a maximum length for a string property.
  • Validation – The base class for all validator attributes.
  • DataType - Additional validations for specific data types, like phone numbers, credit cards and email addresses. Not in the referenced link.

See also https://dataannotationsextensions.apphb.com for additional validators that can be included in your application.

As far as client side tag attributes are concerned, these are processed by the unobtrusive adapters that the above annotations generate. These are prefixed with "data-val-". Additional parameters for the validator would be added as additional attributes. For example: regex becomes data-val-regex="Message" data-val-regex-pattern="some pattern"

From MVC3 jQuery.validate.unobtrusive.js:

adapters.addSingleVal("accept", "exts")
        .addSingleVal("regex", "pattern");

adapters.addBool("creditcard")
        .addBool("date")
        .addBool("digits")
        .addBool("email")
        .addBool("number")
        .addBool("url");

adapters.addMinMax("length", "minlength", "maxlength", "rangelength")
        .addMinMax("range", "min", "max", "range");

adapters.add("equalto", ["other"], function (options) {
        // removed for brevity
});
adapters.add("required", function (options) {
    // removed for brevity
});
adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
    // removed for brevity
});
Iiette answered 2/9, 2015 at 17:45 Comment(1)
I don't think you'll find what you are looking for, and the answer for all the potential combinations is not something I have time to provide. Consider that the DataTypeAttribute provides 17 different enumeration values. Regardless, those will result in one of the adapters listed above. I would suggest adding a view model with the attributes that you need and generating a scaffold to see what it generates.Iiette
C
1

This is an old question and although there is an answer there was still a lack of a simple table describing the different attributes in C# and the matching data attributes. Below is table with this information.

Table

Data Validation Attributes Table

Notes

  • All data validation attributes will create a data attribute of the form data-val-<name>="<message>" and any attributes will be of the form data-val-<name>-<param>="<value>"
  • In the table the HTML Name column represents the name in the data attribute. Eg. data-val-maxlength
  • In the table the Parameter Name column represents the parameter name in the data attribute after the validation name. Eg. data-val-maxlength-max
  • The Message Index column is the index of the parameter in the ErrorMessage, index 0 is always the field name. Eg. ErrorMessage = "The field {0} cannot be longer than {1}"
  • This data is taken from the .NET Core repositories, there may be other attributes provided by other libraries

References

Caravansary answered 29/11, 2018 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.