# Getting Started with the Captcha

This tutorial explains how to set up a basic Telerik UI for ASP.NET Core Captcha and highlights the major steps in the configuration of the component.

You will initialize a Captcha and learn how to add audio reading support. Then, you will see how to attach an event handler to the component. Finally, you can run the sample code in [Telerik REPL](https://netcorerepl.telerik.com/) and continue exploring the component.

![Sample Telerik UI for ASP.NET Core Captcha](https://www.telerik.com/aspnet-core-ui/documentation/assets/2c40ecc48e5fd5d9e17f534fa8ea6992/captcha-getting-started.png)

## Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:

* You can use the [Telerik REPL](https://netcorerepl.telerik.com/) playground and skip installing the components on your system and configuring a project.

* You can prepare a Visual Studio project by following the following guide:

  * Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from [a project template](https://www.telerik.com/aspnet-core-ui/documentation/vs-integration/new-project-wizard).

## 1. Prepare the CSHTML File

The first step is to add the required directives at the top of the `.cshtml` document:

* To use the Telerik UI for ASP.NET Core HtmlHelpers:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

* To use the Telerik UI for ASP.NET Core TagHelpers:

  ```cshtml
  @addTagHelper *, Kendo.Mvc
  ```

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<h4>Captcha with event handler</h4>

<p>

</p>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<h4>Captcha with event handler</h4>

<p>

</p>
```

## 2. Initialize the Captcha

Use the Captcha HtmlHelper or TagHelper to add the component to a page:

* The `Name()` configuration method is mandatory as its value is used for the `id` and the `name` attributes of the Captcha element.
* The `DataCaptchaField()` represents the main field.
* The `DataCaptchaIdField()` represents the ID field.
* The `Handler()` configuration points to the respective Controller action.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<h4>Captcha with event handler</h4>

<p>
  @(Html.Kendo().Captcha()
   .Name("captcha")
   .DataCaptchaField("Captcha")
   .DataCaptchaIdField("CaptchaID")
   .Handler(handler => handler.Action("Reset_Events", "captcha"))
)
</p>

```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<h4>Captcha with event handler</h4>

<p>
   <kendo-captcha name="captcha" 
    datacaptchafield="Captcha"
    datacaptchaidfield="CaptchaID">
      <handler url="@Url.Action("Reset_Events", "captcha")" />
   </kendo-captcha>
</p>
```

## 3. Add Audio Reader

The next step is to configure the Captcha to allow audio capability. You can do that by using the `AudioHandlerFunction()` configuration.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<h4>Captcha with event handler</h4>

<script>
function audioHandler(args) {
    args.success("../shared/UserFiles/captcha/" + args.data.CaptchaID + ".wav");
}
</script>

<p>
  @(Html.Kendo().Captcha()
   .Name("captcha")
   .DataCaptchaField("Captcha")
   .DataCaptchaIdField("CaptchaID")
   .Handler(handler => handler.Action("Reset_Events", "captcha"))
   .AudioHandlerFunction("audioHandler")
)
</p>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<h4>Captcha with event handler</h4>

<script>
function audioHandler(args) {
    args.success("../shared/UserFiles/captcha/" + args.data.CaptchaID + ".wav");
}
</script>

<p>
   <kendo-captcha name="captcha" 
    datacaptchafield="Captcha"
    datacaptchaidfield="CaptchaID">
      <handler url="@Url.Action("Reset_Events", "captcha")" />
         <audio-handler function-handler="audioHandler" />
   </kendo-captcha>
</p>
```

## 4. Handle a Captcha Event

The Captcha exposes a `Change()` event that you can handle and assign specific functions to the component. In this tutorial, you will use the `Change()` event to display a message when the user changes the input value.

```Razor HtmlHelper
@using Kendo.Mvc.UI

<h4>Captcha with event handler</h4>

<script>
function audioHandler(args) {
    args.success("../shared/UserFiles/captcha/" + args.data.CaptchaID + ".wav");
}
function onChange(e) {
    var value = e.sender.value();
    console.log(value);
}
</script>

<p>
  @(Html.Kendo().Captcha()
   .Name("captcha")
   .DataCaptchaField("Captcha")
   .DataCaptchaIdField("CaptchaID")
   .Handler(handler => handler.Action("Reset_Events", "captcha"))
   .AudioHandlerFunction("audioHandler")
   .Events(e => e.Change("onChange"))
)
</p>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<h4>Captcha with event handler</h4>

<script>
function audioHandler(args) {
    args.success("../shared/UserFiles/captcha/" + args.data.CaptchaID + ".wav");
}
function onChange(e) {
    var value = e.sender.value();
    console.log(value);
}
</script>

<p>
   <kendo-captcha name="captcha" 
    datacaptchafield="Captcha"
    datacaptchaidfield="CaptchaID"
    on-change="onChange">
      <handler url="@Url.Action("Reset_Events", "captcha")" />
         <audio-handler function-handler="audioHandler" />
   </kendo-captcha>
</p>
```

For more examples, refer to the [demo on using the events of the Captcha](https://demos.telerik.com/aspnet-core/captcha/events).

## 5. (Optional) Reference Existing Captcha Instances

You can reference the Captcha instances that you have created and build on top of their existing configuration:

1. Use the `id` attribute of the component instance to establish a reference.

   ```JS script
   <script>
       var captchaReference = $("#captcha").data("kendoCaptcha"); // captchaReference is a reference to the existing captcha instance of the helper.
   </script>
   ```

2. Use the [Captcha client-side API](https://docs.telerik.com/kendo-ui/api/ui/captcha#methods) to control the behavior of the widget. In this example, you will use the `reset` method to refresh the captcha.

   ```JS script
   <script>
       var captchaReference = $("#captcha").data("kendoCaptcha"); // captchaReference is a reference to the existing timeCaptcha instance of the helper.
        captchaReference.reset(); // refreshes the Captcha
   </script>
   ```

For more information on referencing specific helper instances, see the [Methods and Events](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/helper-basics/events-and-methods) article.

## Explore this Tutorial in REPL

You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:

* [Sample code with the Captcha HtmlHelper](https://netcorerepl.telerik.com/GRvPYjaC142YAsQv52)
* [Sample code with the Captcha TagHelper](https://netcorerepl.telerik.com/GxPFatuW14a3k4cE22)

## Next Steps

* [Configure the Provider of the Captcha](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/editors/captcha/provider)
* [Configure the Captcha's Validation](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/editors/captcha/validation)

## See Also

* [Using the API of the Captcha for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/captcha/api)
* [Client-Side API of the Captcha](https://docs.telerik.com/kendo-ui/api/ui/captcha)
* [Server-Side API of the Captcha](https://www.telerik.com/aspnet-core-ui/documentation/api/captcha)
* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
