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")]
|