The MinLength validation is helpful for creating constraints on certain fields such as Username, Zip Code, and so on. We will start investigating MinLength validation in this section.
The validation error can be captured in MinLength data annotation in the following scenarios:
- If the field is NULL or empty or whitespace.
-
If the field value length is less than configured. For instance, if the MinLength is configured as 6, then if the number of characters provided in the field is less than 6 the min length validation error would be thrown.
The MinLength attribute/data annotation can be configured as follows:
public class RegistrationViewModel
{
[Required(ErrorMessage = "Username is required")]
[MinLength(6)]
public string Username { get; set; }
// Code removed for brevity
}
If field value...