This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22441|Reply: 0

[ASP.NET] Asp.net Core Model Data Validation

[Copy link]
Posted on 11/21/2019 10:49:51 AM | | | |
The usage is similar to Asp.net MVC. Essentially, a layer of js verification is added to the UI layer and a layer of background verification is added to the controller.
UI layer js validation is to reference two js scripts to work

If this script is not referenced, frontend html page validation does not work.
The verification prompt information is passed through the Tag helper's asp-validation-for, e.g., <span asp-validation-for="Rating" class="text-danger"></span>
If the verification fails, the prompt is automatically added to the span tag text.

Specific examples

1Create a type to verify

The Required attribute indicates that the attribute cannot be null, but it cannot check for spaces
Value types (e.g. decimal, int, float, DateTime) are required in their own right, and do not require the [Required] attribute.
The Range feature limits the value within the specified range.
The StringLength feature allows you to set the maximum length of the string, as well as the optional minimum length.
RegularExpression feature to validate regular expressions.

[Compare("Password", ErrorMessage = "Password entered twice inconsistent")]
[display(Name="Confirm Password")]
[DataType(DataType.Password)]

Common built-in validation attributes:
[CreditCard]: Verify that the attribute is in credit card format
[Compare]: Verify that the two attributes in the model match
[EmailAddress]: Verify that the attribute is in email format
[Phone]: Verify that the attribute is in phone number format
[Range]: Verify that the attribute value is within the given range
[RegularExpression]: Verify that the data matches the specified regular expression
[Required]: Required attributes
[StringLength]: Verify the maximum length of the string property
[url]: Verify that the attribute is in URL format

Note: The use of the Range feature on DateTime for JQuery date validation is prohibited. Because regardless of whether the time value is populated or not, the js validation will give an error.

2 Validate on the controller

3 UI pages



As shown in the image: The screening date prompts an error, precisely due to
The use of Range on the DateTime type is caused, so the use of Range on DateTime is prohibited.

5 Customize the prompt information when data verification fails

The ErrorMessage attribute is used on the validation feature
Such as
[StringLength(30, ErrorMessage="Genre cannot be longer than 30")]





Previous:Gain an in-depth understanding of Redis
Next:Jquery gets all the elements of the form FORM for AJAX submission
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com