I’ve created my model using Entity Framework, and I’ve created a basic website in MVC3 to allow me to edit items within the database. So far it works fine, but I wanted to add a regular expression to ensure that the contents of one of the form fields always contains a particular HTML element.
What I have in my model so far, for the property in question, is as follows:
[Required]
[AllowHtml]
[DataType(DataType.MultilineText)]
[RegularExpression("^.*(<p>).*$", ErrorMessage = "Must contain <p> element")]
public string Text { get; set; }
I’ve tried variations on the RegularExpression attribute shown above, but nothing seems to work. What I essentially want to do is make sure that the text in the text area for the above property always contains at least one <p> element.
Admittedly I’m not very strong on regular expressions, so I’m probably way off the mark. Any pointers? Thanks.
Try this, which makes sure that there is an opening and closing
<p>tag (so they can’t just do<p>without also doing</p>).There is a handy explanation here, which will help you customise the example should you wish to.