I am really bad with regexps, what I want is a regexp that does not match any html tags (for user input validation).
What I want is negative of this:
<[^>]+>
What I currently have is
public class MessageViewModel
{
[Required]
[RegularExpression(@"<[^>]+>", ErrorMessage = "No html tags allowed")]
public string UserName { get; set; }
}
but it does opposite of what I want - allows usernames only with html tags
Regular expressions cannot do "negative" matches.
But they can do "positive" matches and you can then throw out of the string everything that they have found.
Edit - after the question was updated, things became a little clearer. Try this:
public class MessageViewModel
{
[Required]
[RegularExpression(@"^(?!.*<[^>]+>).*", ErrorMessage = "No html tags allowed")]
public string UserName { get; set; }
}
Explanation:
^ # start of string
(?! # negative look-ahead (a position not followed by...)
.* # anything
<[^>]+> # something that looks like an HTML tag
) # end look-ahead
.* # match the remainder of the string