##MaxLength Validator
確保特定字串屬性的長度不超過指定值。僅對字串有效properties字串格式參數:{PropertyName} = 正在驗證的屬性的名稱{MaxLength} = 最大長度{TotalLength} =輸入的字元數{PropertyValue} = 屬性的目前值 MinLength 驗證器 確保特定字串屬性的長度大於指定值。 僅對字串屬性有效{PropertyName} = 正在驗證的屬性的名稱{MinLength} = 最小長度{TotalLength} =輸入的字元數{ PropertyValue} = 屬性的目前值範例static void Main(string[] args){ List errors = new List(); PersonModel person = new PersonModel(); person.FirstName = "TestUser444"; person.LastName = "TTT"; PersonValidator validator = new PersonValidator(); ValidationResult results = validator.Validate(person); if (results.IsValid == false){ foreach (ValidationFailure failure in results.Errors){ errors.Add(failure.ErrorMessage); } } foreach (var item in errors){ Console.WriteLine(item); } Console.ReadLine(); } } public class PersonModel{ public string FirstName { get; set; } public string LastName { get; set; } } public class PersonValidator : AbstractValidator{ public PersonValidator(){ RuleFor(p => p.FirstName).MaximumLength(7).WithMessage("MaximumLength must be 7 {PropertyName}") ; RuleFor(p => p.LastName).MinimumLength(5).WithMessage("MinimumLength must be 5 {PropertyName}"); } }
MaximumLength must be 7 First Name MinimumLength must be 5 Last Name
以上是如何使用 Fluent Validation 檢查 C# 中屬性的最小長度和最大長度驗證?的詳細內容。更多資訊請關注PHP中文網其他相關文章!