# Validating Properties Decorated with the Compare Attribute?

## Environment

<table>
	<tbody>
		<tr>
			<td>Product Version</td>
			<td>2022.3.913</td>
		</tr>
		<tr>
			<td>Product</td>
			<td>Form for Telerik UI for ASP.NET MVC</td>
		</tr>
	</tbody>
</table>

## Description

How can I validate properties decorated with the `Compare` attribute when using the Form? For example:

```csharp
    public class User
    {
        [Display(Name = "UserName")]
        public string UserName { get; set; }

        [Required]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        [StringLength(20, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
        public string? Password { get; set; };

        [Required]
        [Display(Name = "Confirm Password")]
        [DataType(DataType.Password)]
        [NotMapped]
        [Compare("Password", ErrorMessage = "Passwords did not match.")]
        public string? ConfirmPassword { get; set; }
    }
```

## Solution

You can achieve this by extending the Form's built-in Kendo Validator:

```Razor View
    @(Html.Kendo().Form<User>()
        .Name("myForm")
        .HtmlAttributes(new { action = @Url.Action("MyAction","MyController"), method = "POST" })
        .Validatable(v=>v.ValidateOnBlur(true))
        .Items(items => {
                items.Add().Field(f => f.UserName);
                items.Add().Field(f => f.Password);
                items.Add().Field(f => f.ConfirmPassword);
            })
        )
    <script>
        (function ($, kendo) {
        $.extend(true, kendo.ui.validator, {
            rules: {
                equalto: function (input) {
                    if (input.filter("[data-val-equalto-other]").length) {
                        var otherField = input.attr("data-val-equalto-other");
                        otherField = otherField.substr(otherField.lastIndexOf(".") + 1);

                        return input.val() == $("#" + otherField).val();
                    }
                    return true;
                }
            },
            messages: {
                equalto: function (intput) {
                    return intput.attr("data-val-equalto");
                }
            }
        });
    })(jQuery, kendo);
    </script>
```

```C# Controller
    public ActionResult Index()
    {
        var viewModel = new User();
        return View(viewModel);
    }
```

## More ASP.NET MVC Form Resources

* [ASP.NET MVC Form Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/layout/form/overview)

* [ASP.NET MVC Form Demos](https://demos.telerik.com/aspnet-mvc/form)

* [ASP.NET MVC Form Product Page](https://www.telerik.com/aspnet-mvc/form)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Form for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/form)
* [Server-Side API Reference of the Form for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/form)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
