Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string contains HTML code in ASP.NET MVC model validation?

I have a model string which looks like:

bla bla bla <b>asdad</b> bla bla bla bla <u>bla</u> ...

My model:

public class MyModel {
      [RegularExpression( @"^(<\s*([^ >]+)[^>]*>.*?<\s*\/\s*\1\s*>)$", ErrorMessage = "No tag is allowed !")]
      public string Text { get; set; }
}

I tried to negate above regex ( I know that I didn't use correctly and I don't know how to do this correctly)

I want to show error when Text contains any match of HTML code, even it has no closed tag, means should occur when met:

<b> without </b>

</b> or similar

How to achieve this with regex ?

like image 273
Snake Eyes Avatar asked Sep 26 '22 05:09

Snake Eyes


1 Answers

This is the regex for that:

<(\s*[(\/?)\w+]*)

It checks for even if single closing tag is there or opening tag is there, it matches that.

DEMO here

like image 82
Ehsan Sajjad Avatar answered Nov 15 '22 06:11

Ehsan Sajjad