---
title: "Quick Tip: How to prevent HTML in Umbraco Form fields — Hack Make Do"
description: "Stop HTML being submitted through Umbraco Forms fields, using a FormValidateNotification handler and a regular expression."
url: https://hackmakedo.com/blog/quick-tip-how-to-prevent-html-in-umbraco-form-fields/
---

# Quick Tip: How to prevent HTML in Umbraco Form fields

9 April 2025

- [Tutorials](https://hackmakedo.com/blog/topic/tutorials/)
- [Umbraco Forms](https://hackmakedo.com/blog/topic/umbraco-forms/)

Howdy\
I just wanted to shared a quick code snippet with you all. I have been working for a client where they needed to ensure no HTML could be submitted to a form using Umbraco forms.

Unfortunately out of the box, Umbraco Forms does not support this feature, but with the documentation and extension points available to us, we are able to ensure we apply validation to prevent HTML being submitted into an Umbraco Form.

But with [Umbraco Forms FormValidateNotification](https://docs.umbraco.com/umbraco-forms/developer/extending/adding-an-event-handler#form-validation-notification) we are able to validate the value being submitted to a form field and using RegEx ensure that it does not contain HTML, if so it will return a validation error message back to the user on the form.

## Show me the code

### Forms Validate Notification Handler

```csharp
/// <summary>
/// Referred from Docs
/// https://docs.umbraco.com/umbraco-forms/developer/extending/adding-an-event-handler
/// </summary>
public class FormValidateNotificationHandler : INotificationHandler<FormValidateNotification>
{
	public void Handle(FormValidateNotification notification)
	{

		var form = notification.Form;
		var httpCtx = notification.Context;

		if (notification.ModelState.IsValid == false)
		{
			return;
		}

		if (httpCtx.Request.HasFormContentType is false)
		{
			return;
		}

		foreach(var field in form.AllFields)
		{
			if(field is not null)
			{
				if (FormDoesNotContainField(field, httpCtx))
				{
					continue;
				}

				var postedValue = httpCtx.Request.Form[field.Id.ToString()].ToString().Trim();

				if (ContainsHtml(postedValue))
				{
					notification.ModelState.AddModelError(field.Id.ToString(), "HTML is not allowed");
				}
			}
		}
	}

	private bool FormDoesNotContainField(Field field, HttpContext httpCtx) => httpCtx.Request.Form.Keys.Contains(field.Id.ToString()) is false;

	// https://uibakery.io/regex-library/html-regex-csharp
	private readonly static Regex _htmlTagRegex = new Regex("<(?:\"[^\"]*\"['\"]*|'[^']*'['\"]*|[^'\">])+>", RegexOptions.Compiled);

	private bool ContainsHtml(string input) => _htmlTagRegex.IsMatch(input);
}
```

## Keep reading

3 January 2023

### [Resolving the Swagger conflict for Swashbuckle and NSwag in Umbraco Forms 10.2+](https://hackmakedo.com/blog/resolving-the-swagger-conflict-for-swashbuckle-and-nswag-in-umbraco-forms-10-2/)

Fix the Swagger UI conflict between Swashbuckle and NSwag on Umbraco sites running Umbraco Forms 10.2. Umbraco has since fixed it in Forms 10.2.2 and 11.0.1.

[Tutorials](https://hackmakedo.com/blog/topic/tutorials/) · [.NET](https://hackmakedo.com/blog/topic/dotnet/) · [Umbraco Forms](https://hackmakedo.com/blog/topic/umbraco-forms/)

28 April 2025

### [Using SignalR, IOptionsMonitor with Umbraco Bellissima for reactive .NET Options](https://hackmakedo.com/blog/using-signalr-ioptionsmonitor-with-umbraco-bellissima-for-reactive-net-options/)

Combine IOptionsMonitor with a SignalR hub in an Umbraco 14+ package, so changing a .NET option switches features on or off in the backoffice with no redeploy.

[Bellissima](https://hackmakedo.com/blog/topic/bellissima/) · [.NET](https://hackmakedo.com/blog/topic/dotnet/) · [Tutorials](https://hackmakedo.com/blog/topic/tutorials/)

Need a hand with Umbraco Bellissima? I'm available as a freelancer or contractor.

[Get in touch](https://hackmakedo.com/contact/)
