简体   繁体   中英

Using DataAnnotations with Regular Expressions to not Match

Is it possible with DataAnnotation s and regular expressions to filter an entry in the text box ? ie To trigger an invalid response when the word "apt" is in the string ?

Yes you can do that using negative look-arounds, like this:

public class MyModel
{
    [RegularExpression(@"^((?!apt).)*$", ErrorMessage = "You can not have that")]
    public string MyValue { get; set; }
}

Here is a good reference question for these types of regular expressions.

Regular expression to match string not containing a word?

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