You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/mvc/models/validation.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,7 @@ Validation attributes let you specify validation rules for model properties. The
45
45
46
46
Here are some of the built-in validation attributes:
47
47
48
-
*`[CreditCard]`: Validates that the property has a credit card format.
48
+
*`[CreditCard]`: Validates that the property has a credit card format. Requires [jQuery Validation Additional Methods](https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js).
49
49
*`[Compare]`: Validates that two properties in a model match.
50
50
*`[EmailAddress]`: Validates that the property has an email format.
51
51
*`[Phone]`: Validates that the property has a telephone number format.
@@ -112,7 +112,7 @@ The `[Remote]` attribute implements client-side validation that requires calling
112
112
113
113
To implement remote validation:
114
114
115
-
1. Create an action method for JavaScript to call. The jQuery Validate[remote](https://jqueryvalidation.org/remote-method/) method expects a JSON response:
115
+
1. Create an action method for JavaScript to call. The jQuery Validation[remote](https://jqueryvalidation.org/remote-method/) method expects a JSON response:
116
116
117
117
*`true` means the input data is valid.
118
118
*`false`, `undefined`, or `null` means the input is invalid. Display the default error message.
@@ -238,7 +238,7 @@ Client-side validation avoids an unnecessary round trip to the server when there
The [jQuery Unobtrusive Validation](https://github.com/aspnet/jquery-validation-unobtrusive) script is a custom Microsoft front-end library that builds on the popular [jQuery Validate](https://jqueryvalidation.org/) plugin. Without jQuery Unobtrusive Validation, you would have to code the same validation logic in two places: once in the server-side validation attributes on model properties, and then again in client-side scripts. Instead, [Tag Helpers](xref:mvc/views/tag-helpers/intro) and [HTML helpers](xref:mvc/views/overview) use the validation attributes and type metadata from model properties to render HTML 5 `data-` attributes for the form elements that need validation. jQuery Unobtrusive Validation parses the `data-` attributes and passes the logic to jQuery Validate, effectively "copying" the server-side validation logic to the client. You can display validation errors on the client using tag helpers as shown here:
241
+
The [jQuery Unobtrusive Validation](https://github.com/aspnet/jquery-validation-unobtrusive) script is a custom Microsoft front-end library that builds on the popular [jQuery Validation](https://jqueryvalidation.org/) plugin. Without jQuery Unobtrusive Validation, you would have to code the same validation logic in two places: once in the server-side validation attributes on model properties, and then again in client-side scripts. Instead, [Tag Helpers](xref:mvc/views/tag-helpers/intro) and [HTML helpers](xref:mvc/views/overview) use the validation attributes and type metadata from model properties to render HTML 5 `data-` attributes for the form elements that need validation. jQuery Unobtrusive Validation parses the `data-` attributes and passes the logic to jQuery Validation, effectively "copying" the server-side validation logic to the client. You can display validation errors on the client using tag helpers as shown here:
@@ -255,7 +255,7 @@ The preceding tag helpers render the following HTML:
255
255
</div>
256
256
```
257
257
258
-
Notice that the `data-` attributes in the HTML output correspond to the validation attributes for the `Movie.ReleaseDate` property. The `data-val-required` attribute contains an error message to display if the user doesn't fill in the release date field. jQuery Unobtrusive Validation passes this value to the jQuery Validate[required()](https://jqueryvalidation.org/required-method/) method, which then displays that message in the accompanying **\<span>** element.
258
+
Notice that the `data-` attributes in the HTML output correspond to the validation attributes for the `Movie.ReleaseDate` property. The `data-val-required` attribute contains an error message to display if the user doesn't fill in the release date field. jQuery Unobtrusive Validation passes this value to the jQuery Validation[required()](https://jqueryvalidation.org/required-method/) method, which then displays that message in the accompanying **\<span>** element.
259
259
260
260
Data type validation is based on the .NET type of a property, unless that is overridden by a `[DataType]` attribute. Browsers have their own default error messages, but the jQuery Validation Unobtrusive Validation package can override those messages. `[DataType]` attributes and subclasses such as `[EmailAddress]` let you specify the error message.
261
261
@@ -265,7 +265,7 @@ For information on unobtrusive validation, see [this GitHub issue](https://githu
265
265
266
266
### Add Validation to Dynamic Forms
267
267
268
-
jQuery Unobtrusive Validation passes validation logic and parameters to jQuery Validate when the page first loads. Therefore, validation doesn't work automatically on dynamically generated forms. To enable validation, tell jQuery Unobtrusive Validation to parse the dynamic form immediately after you create it. For example, the following code sets up client-side validation on a form added via AJAX.
268
+
jQuery Unobtrusive Validation passes validation logic and parameters to jQuery Validation when the page first loads. Therefore, validation doesn't work automatically on dynamically generated forms. To enable validation, tell jQuery Unobtrusive Validation to parse the dynamic form immediately after you create it. For example, the following code sets up client-side validation on a form added via AJAX.
269
269
270
270
```javascript
271
271
$.get({
@@ -284,7 +284,7 @@ $.get({
284
284
})
285
285
```
286
286
287
-
The `$.validator.unobtrusive.parse()` method accepts a jQuery selector for its one argument. This method tells jQuery Unobtrusive Validation to parse the `data-` attributes of forms within that selector. The values of those attributes are then passed to the jQuery Validate plugin.
287
+
The `$.validator.unobtrusive.parse()` method accepts a jQuery selector for its one argument. This method tells jQuery Unobtrusive Validation to parse the `data-` attributes of forms within that selector. The values of those attributes are then passed to the jQuery Validation plugin.
$(form).removeData("validator") // Added by jQuery Validate
303
+
$(form).removeData("validator") // Added by jQuery Validation
304
304
.removeData("unobtrusiveValidation"); // Added by jQuery Unobtrusive Validation
305
305
$.validator.unobtrusive.parse(form);
306
306
}
@@ -309,11 +309,11 @@ $.get({
309
309
310
310
## Custom client-side validation
311
311
312
-
Custom client-side validation is done by generating `data-` HTML attributes that work with a custom jQuery Validate adapter. The following sample adapter code was written for the `[ClassicMovie]` and `[ClassicMovieWithClientValidator]` attributes that were introduced earlier in this article:
312
+
Custom client-side validation is done by generating `data-` HTML attributes that work with a custom jQuery Validation adapter. The following sample adapter code was written for the `[ClassicMovie]` and `[ClassicMovieWithClientValidator]` attributes that were introduced earlier in this article:
0 commit comments