(Asp.Net MVC3) 如何定义 ASCII 和 Unicode 的 StringLength?

发布于 2024-12-17 08:54:19 字数 452 浏览 2 评论 0原文

我在模型中有以下定义

        [Required]
        [StringLength(100, MinimumLength = 10)]
        [DataType(DataType.Text)]
        [Display(Name = "XXX")]
        public string XXX{ get; set; }

现在我希望它以不同的方式对待 ACSII 和 Unicode 输入,对于 ASCII,每个字符考虑长度 1,所以需要最小长度 10 和最大长度 50。但是对于 Unicode 字符,我想考虑它长度 2,所以 5 个 unicode 字符足以满足最低要求。

我该怎么做?

我想我可能需要两种方法,首先覆盖asp.net中的长度检查,然后我需要覆盖jquery中的长度检查。不是吗?

这里有人有工作样本吗,谢谢。

I have following define in model

        [Required]
        [StringLength(100, MinimumLength = 10)]
        [DataType(DataType.Text)]
        [Display(Name = "XXX")]
        public string XXX{ get; set; }

Now I want it treat ACSII and Unicode input differently, for ASCII, each char consider length 1, so need min-length 10 and max length 50. But for Unicode char, I want to consider it length 2, so 5 unicode chars is enough for the minimal requirement.

How do I do it?

I guess I might need two approach, first overwrite the length check in asp.net, then I need to overwite the length check in jquery. Isn't it?

Is anyone here have a working sample, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-12-24 08:54:19

要执行您想要的操作,您应该能够引入自定义验证属性:

class FooAttribute : ValidationAttribute
{
    private readonly int minLength, maxLength;
    public FooAttribute(int minLength, int maxLength) : this(minLength, maxLength, "Invalid ASCII/unicode string-length") {}
    public FooAttribute(int minLength, int maxLength, string errorMessage) : base(errorMessage)
    {
        this.minLength = minLength;
        this.maxLength = maxLength;
    }

    protected override ValidationResult IsValid(object value, ValidationContext ctx)
    {
        if(value == null) return ValidationResult.Success;
        var s = value as string;
        if(s == null) return new ValidationResult("Not a string");

        bool hasNonAscii = s.Any(c => c >= 128);

        int effectiveLength = hasNonAscii ? (2*s.Length) : s.Length;

        if(effectiveLength < minLength || effectiveLength > maxLength) return new ValidationResult(FormatErrorMessage(ctx.DisplayName));
        return ValidationResult.Success;
    }
}

To do what you want, you should be able to introduce a custom validation attribute:

class FooAttribute : ValidationAttribute
{
    private readonly int minLength, maxLength;
    public FooAttribute(int minLength, int maxLength) : this(minLength, maxLength, "Invalid ASCII/unicode string-length") {}
    public FooAttribute(int minLength, int maxLength, string errorMessage) : base(errorMessage)
    {
        this.minLength = minLength;
        this.maxLength = maxLength;
    }

    protected override ValidationResult IsValid(object value, ValidationContext ctx)
    {
        if(value == null) return ValidationResult.Success;
        var s = value as string;
        if(s == null) return new ValidationResult("Not a string");

        bool hasNonAscii = s.Any(c => c >= 128);

        int effectiveLength = hasNonAscii ? (2*s.Length) : s.Length;

        if(effectiveLength < minLength || effectiveLength > maxLength) return new ValidationResult(FormatErrorMessage(ctx.DisplayName));
        return ValidationResult.Success;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文