# Validate a Selection of Files for Upload

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Upload</td>
 </tr>
 <tr>
  <td>Operating System</td>
  <td>All</td>
 </tr>
 <tr>
  <td>Browser</td>
  <td>All</td>
 </tr>
 <tr>
  <td>Browser Version</td>
  <td>All</td>
 </tr>
</table>

## Description

How can I validate if a file is selected for upload when working with the Telerik UI for ASP.NET MVC Upload?

## Solution

* Initialize the Kendo UI Validator and provide a custom rule which validates whether a file in the Upload is selected.

```
      var validator = $("#myForm").kendoValidator({
            rules: {
            upload: function(input) {
                if (input.attr('name') == "files") {
                    var uploadControl = $("#files").data("kendoUpload"); //get a reference of the Upload component
                    var uploadedFiles = uploadControl.getFiles(); //get the uploaded files

                    return uploadedFiles.length > 0; //assert if there are any files selected
                }

                return true;
              }
            }
        }).data("kendoValidator");
```

* Create the Upload component within the **Form**.

```
    <form id="myForm" method="post">
        @(Html.Kendo().Upload()
            .Name("files")
            .Messages(m => m.Select("Select Files"))
            .HtmlAttributes(new {validationmessage = "File is required" })
            .Validation(validation => validation
               .MaxFileSize(10485760)
               .MinFileSize(30720)
            )
       )
       <span class="k-invalid-msg" data-for="files"></span>
       <input type="submit" id="submitForm" class="k-button" />
    </form>
```

**Validate** on form submission whilst preventing the default behavior.

```
    $(document).ready(function () {
       $("#submitForm").on("click", function (e) {
           e.preventDefault();
           var validator = $("#myForm").data("kendoValidator");
           if (!validator.validate()) {
            $("#validationSummary").html("<div class='k-messagebox k-messagebox-error'>Oops! There is invalid data in the form.</div>");
           }
           else {
            $("#validationSummary").html("<div class='k-messagebox k-messagebox-success'>Hooray! Your form is valid!</div>");
           }
       });
    });
```

For the complete example, refer to the following [Telerik REPL](https://netcorerepl.telerik.com/wwYPwobp35zcEZ9d17) example:

```Razor Index.cshtml
    <div id="validationSummary"></div>

    <form id="myForm" method="post">
        @(Html.Kendo().Upload()
            .Name("files")
            .Messages(m => m.Select("Select Files"))
            .HtmlAttributes(new {validationmessage = "File is required" })
            .Validation(validation => validation
            .MaxFileSize(10485760)
            .MinFileSize(30720))
        )

        <span class="k-invalid-msg" data-for="files"></span>
        <input type="submit" id="submitForm" />
    </form>
```

```JS script.js
    <script>
          var validator = $("#myForm").kendoValidator({
                rules: {
                upload: function(input) {
                    if (input.attr('name') == "files") {
                        var uploadControl = $("#files").data("kendoUpload");
                        var uploadedFiled = uploadControl.getFiles();

                        return uploadedFiled.length > 0;
                    }

                    return true;
                  }
                }
            }).data("kendoValidator");

        $(document).ready(function () {
            $("#submitForm").on("click", function (e) {
                e.preventDefault();
                var validator = $("#myForm").data("kendoValidator");
                if (!validator.validate()) {
                    $("#validationSummary").html("<div class='k-messagebox k-messagebox-error'>Oops! There is invalid data in the form.</   div>");
                }
                else {
                    $("#validationSummary").html("<div class='k-messagebox k-messagebox-success'>Hooray! Your form is valid!</div>");
                }
            });
        });
    </script>
```

## More ASP.NET MVC Upload Resources

* [ASP.NET MVC Upload Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/upload/overview)

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

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

* [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 Upload for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/upload)
* [Server-Side API Reference of the Upload for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/upload)
* [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)
