# AJAX forms events

> info **PREREQUISITES:** You have enabled the AJAX submit call functionality for the Forms module. For more information, see [Submit forms using AJAX call](https://www.progress.com/documentation/sitefinity-cms/for-developers-submit-forms-using-ajax-call).

## IFormValidatingEvent

The following interface is implemented by all events raised before a form is validated. There is only one such built-in event `FormValidatingEvent`and you must subscribe to the interface rather than the class that implements it. Subscribe to the event using the following code:

```C#
using System;
using System.Linq;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Modules.Forms.Events;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Events;

namespace SitefinityWebApp
{
   public class Global : System.Web.HttpApplication
   {
       protected void Application_Start(object sender, EventArgs e)
       {
           Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
       }

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           EventHub.Subscribe<IFormValidatingEvent>(evt => FormValidatingEventHandler(evt));
       }

       public void FormValidatingEventHandler(IFormValidatingEvent evt)
       {
           //check if all the input is valid

           if (evt.FormName == "MyForm")
           {
               var field = evt.Controls.SingleOrDefault(control => control.FieldName == "Name");
               if (field != null)
               {
                   var name = (string)field.Value;
                   if (name.Split(' ').Length > 2)
                       throw new ValidationException("The 'Name' field must consist of two words.");
               }
           }
       }
   }
}
```

The above example demonstrates how to check for invalid input.\
As a result, if you attempt to submit the form with a value that does not match the validation criteria, an error message *The 'Name' field must consist of two words.* is displayed.

In the event handler, you can access the following information:

* The ID of the user submitting the form.
* The user name of the user submitting the form.
* The IP address of the client that submits the form entry.
* The ID of the form being submitted.
* The unique (code) name of the form being submitted.
* The title of the form being submitted.
* The origin of the event.
* A collection of the form controls that you can use to retrieve the forms' title, values, and so forth.

##

IFormFieldValidatingEvent

This interface is implemented by the `FormFieldValidatingEvent`and is raised when a form field is validated. Subscribe to the event using the following code:

```C#
using System;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Modules.Forms.Events;
using Telerik.Sitefinity.Services;

namespace SitefinityWebApp
{
   public class Global : System.Web.HttpApplication
   {
       protected void Application_Start(object sender, EventArgs e)
       {
           Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
       }

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           EventHub.Subscribe<IFormFieldValidatingEvent>(evt => FormFieldValidatingEventHandler(evt));
       }

       public void FormFieldValidatingEventHandler(IFormFieldValidatingEvent eventInfo)
       {
           var origin = eventInfo.Origin;
           var field = eventInfo.FormFieldControl;
       }
   }
}
```

In the event handler, you can access the following information:

* The origin of the event.
* The field that is being validated.
