# XHTML Validation in HtmlTextBox

The HtmlTextBox requires you to provide valid XHTML and properly handle the content you set as its value.

To validate the defined content of the HtmlTextBox, use the `IsValidXhtml` expressions function or the [Telerik.Reporting.Processing.XhtmlValidator.IsValidXhtml](https://www.telerik.com/products/reporting/documentation/api/telerik.reporting.processing.xhtmlvalidator#Telerik_Reporting_Processing_XhtmlValidator_IsValidXhtml_System_String_) static method.

Specifically, the HtmlTextBox supports the following approaches for validating its XHTML content:

* [Using the `IsValidXhtml` method in an expression](https://www.telerik.com/products/reporting/documentation/report-items/htmltextbox/xhtml-validation#using-the-isvalidxhtml-method-in-expression).
* [Using an event and the `IsValidXhtml` method](https://www.telerik.com/products/reporting/documentation/report-items/htmltextbox/xhtml-validation#using-events-and-the-isvalidxhtml-method).
* [Using events and the `ValueError` exception](https://www.telerik.com/products/reporting/documentation/report-items/htmltextbox/xhtml-validation#using-events-and-the-valueerror-exception).

## Using the IsValidXhtml Method in Expression

The following example shows how to use the `IsValidXhtml` method inside the HtmlTextBox `Expression`:

```C#

const string validXhtml = "<b>valid xhtml</b>.";
const string systemXhtml = "Provided html is not acceptable.";

Telerik.Reporting.HtmlTextBox txt = new Telerik.Reporting.HtmlTextBox();
txt.Value = string.Format("=If(IsValidXhtml('{0}'), '{0}', '{1}')", validXhtml, systemXhtml);
```

```VB.NET

Const validXhtml As String = "<b>valid xhtml</b>."
Const systemXhtml As String = "Provided html is not acceptable."

Dim txt As New Telerik.Reporting.HtmlTextBox()
txt.Value = String.Format("=If(IsValidXhtml('{0}'), '{0}', '{1}')", validXhtml, systemXhtml)
```

## Using Events and the IsValidXhtml Method

The following example demonstrates how to apply the `IsValidXhtml` method inside the HtmlTextBox `ItemDataBinding` event handler:

```C#

const string validXhtml = "<b>valid xhtml</b>.";
const string systemXhtml = "Provided html is not acceptable.";

Telerik.Reporting.HtmlTextBox txt = new Telerik.Reporting.HtmlTextBox();
txt.ItemDataBinding += delegate(object sender, EventArgs args)
    {
        Telerik.Reporting.Processing.HtmlTextBox procTxt = (Telerik.Reporting.Processing.HtmlTextBox)sender;
        if (Telerik.Reporting.Processing.XhtmlValidator.IsValidXhtml(validXhtml))
        {
            procTxt.Value = validXhtml;
        }
        else
        {
            procTxt.Value = systemXhtml;
        }
    };
```

```VB.NET
Const validXhtml As String = "<b>valid xhtml</b>."
Const systemXhtml As String = "Provided html is not acceptable."

Public Sub TestValidateXhtmlWithEventAndIsValidXhtml()
    Dim txt As New Telerik.Reporting.HtmlTextBox()
    AddHandler txt.ItemDataBinding, AddressOf MyEventHandler
End Sub

Private Sub MyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
    Dim procTxt As Telerik.Reporting.Processing.HtmlTextBox = DirectCast(sender, Telerik.Reporting.Processing.HtmlTextBox)
    If Telerik.Reporting.Processing.XhtmlValidator.IsValidXhtml(validXhtml) Then
        procTxt.Value = validXhtml
    Else
        procTxt.Value = systemXhtml
    End If
End Sub
```

## Using Events and the ValueError Exception

The following try-catch-block approach shows how to handle the exception:

```C#

const string invalidXhtml = "<b>invalid xhtml.";
const string systemXhtml = "Provided html is not acceptable.";

Telerik.Reporting.HtmlTextBox txt = new Telerik.Reporting.HtmlTextBox();
txt.ItemDataBinding += delegate(object sender, EventArgs args)
    {
        Telerik.Reporting.Processing.HtmlTextBox procTxt = (Telerik.Reporting.Processing.HtmlTextBox)sender;
        try
        {
            procTxt.Value = invalidXhtml;
            // html that will not be accepted
        }
        catch (Exception)
        {
            procTxt.Value = systemXhtml;
        }
    };
```

```VB.NET
Const invalidXhtml As String = "<b>invalid xhtml."
Const defaultXhtml As String = "Provided html is not acceptable."

Public Sub TestValidateXhtmlWithEventAndValueError()
    Dim txt As New Telerik.Reporting.HtmlTextBox()
    AddHandler txt.ItemDataBinding, AddressOf ValueErrorHandler
End Sub

Private Sub ValueErrorHandler(ByVal sender As Object, ByVal e As EventArgs)
    Dim procTxt As Telerik.Reporting.Processing.HtmlTextBox = DirectCast(sender, Telerik.Reporting.Processing.HtmlTextBox)
    Try
        ' html that will not be accepted
        procTxt.Value = invalidXhtml
    Catch generatedExceptionName As Exception
        procTxt.Value = defaultXhtml
    End Try
End Sub
```

## See Also

* [HtmlTextBox Class API Reference](https://www.telerik.com/products/reporting/documentation/api/telerik.reporting.htmltextbox)
* [Demo Page for Telerik Reporting](https://demos.telerik.com/reporting)
* [Telerik Reporting Knowledge Base](https://www.telerik.com/products/reporting/documentation/knowledge-base)
