I new to Regular Expressions in MVC DataAnnotations. I have a form that has a field named Option. The option must start with CA-. I wrote the Regular Expression in different ways to validate this field and I can get it to work. I tried all this:
[RegularExpression(@"^CA-")]
[RegularExpression(@"/CA-/")]
[RegularExpression(@"^[C]+[A]+[-]")]
[RegularExpression(@"^CA-*")]
none of this work. What is wrong with my code? Thank you.
public class CA_OptionsMetadata
{
[RegularExpression(@"^CA-", ErrorMessage = "The Option must start with CA-")]
[Required(ErrorMessage = "Option is Required")]
public string Option { get; set; }
//public string Cap_LBS { get; set; }
//public string Cap_KG { get; set; }
}
See Question&Answers more detail:os