How do I add a multi-line parameter to the RegularExpression attribute?

I use:



to make sure each line of the multiline text box is matched correctly. However, I cannot figure out how to add the global flag and multline flag options. Is this not possible with MVC? What other options do I have?


source to share

3 answers

Doesn't seem to RegularExpressionAttribute

support run parameters, so here it allows (compile checked but not checked):

[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);




You can add an inline option to enable MultiLine without adding the RegexOptions overload to the attribute. This will also ensure that the expression will work in Javascript as well.





This is how you could do it with Regex, basically without relying on a multi-line flag or attribute, instead you explicitly define a regex to allow newlines, but the same pattern should follow





All Articles