如何在RegularExpression属性上添加多行选项? [英] How to add multiline option on RegularExpression attribute?

查看:72
本文介绍了如何在RegularExpression属性上添加多行选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

[RegularExpression(@"^(""|\[)?[a-zA-Z0-9']{1,125}(""|\])?$")]

确保多行文本框的每一行都正确匹配.但是,我无法弄清楚如何添加全局标志和多行标志选项.用MVC不可能吗?我还有什么其他选择?

to make sure each line of a multiline textbox is properly matched. I cannot however figure out how to add the global flag and multline flag options. Is it impossible with MVC? What other options do I have?

推荐答案

它看起来不像

It doesn't look like the RegularExpressionAttribute supports passing options, so here's one that allows it (compile checked but not tested):

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, 
                         AllowMultiple = false)]
public class RegExAttribute : ValidationAttribute
{
    public string Pattern { get; set; }
    public RegexOptions Options { get; set; }

    public RegExAttribute(string pattern) : this(pattern, RegexOptions.None) { }
    public RegExAttribute(string pattern, RegexOptions options)
    {
        Pattern = pattern;
        Options = options;
    }

    public override bool IsValid(object value)
    {
        return Regex.IsMatch(value.ToString(), Pattern, Options);
    }
}

这篇关于如何在RegularExpression属性上添加多行选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆