gpt4 book ai didi

c# - 只应验证最小/最大长度

转载 作者:行者123 更新时间:2023-12-04 14:00:57 25 4
gpt4 key购买 nike

我在我的 ASP.NET MVC 4 项目中使用 FluentValidation 框架进行服务器端和客户端验证。
有没有本地 (非黑客)仅使用最大长度或仅最小长度验证字符串长度的方法?
例如这种方式:

var isMinLengthOnly = true;
var minLength = 10;
RuleFor(m => m.Name)
.NotEmpty().WithMessage("Name required")
.Length(minLength, isMinLengthOnly);
默认错误消息模板不应该 'Name' must be between 10 and 99999999 characters. You entered 251 characters.'Name' must be longer than 10 characters. You entered 251 characters.并且应该支持客户端属性,例如黑客喜欢 RuleFor(m => m.Name.Length).GreaterThanOrEqual(minLength) (不确定它是否有效)不适用。

最佳答案

您可以使用

RuleFor(x => x.ProductName).NotEmpty().WithMessage("Name required")
.Length(10);

获取消息
'Name' must be longer 10 characters. You entered 251 characters.

如果你想检查最小和最大长度
RuleFor(x => x.Name).NotEmpty().WithMessage("Name required")
.Must(x => x.Length > 10 && x.Length < 15)
.WithMessage("Name should be between 10 and 15 chars");

关于c# - 只应验证最小/最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com