I need a reference list of all unobtrusive validation attributes for each Validation Attribute. Something like:
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
});
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
Notes
data-val-<name>="<message>"
and any attributes will be of the form data-val-<name>-<param>="<value>"
data-val-maxlength
data-val-maxlength-max
ErrorMessage = "The field {0} cannot be longer than {1}"
References
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With