简体   繁体   中英

Validation of regular expressions

Need to verify data input with the attribute RegularExpression , while on a property goes several regular expressions, for example:

     [RegularExpression (@ "[a-z] {4,} ", ErrorMessage ="Short Name")]
     [RegularExpression (@ "[a-z]", ErrorMessage = "invalid character ")]
     public string Name {set; get;}

The use of two attributes RegularExpression compiler issues "Duplicate 'RegularExpression' attribute".

how to implement a verification?

You can create custom attributes:

public class ShortNameAttribute : RegularExpressionAttribute
{
    public ShortNameAttribute() : base(@"[a-z] {4,} ")
    {
    }
}
public class InvalidCharsAttribute : RegularExpressionAttribute
{
    public InvalidCharsAttribute() : base(@"[a-z]")
    {
    }
}


[ShortNameAttribute]
[InvalidCharsAttribute] 
public string Name { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM