Jul 10, 2018

ASP.NET Core custom validation for required on file upload or radio - dirty way

In some cases like radio group or file upload buttons standard Required validation with JQuery don't work.
There is legit way of registering custom validation attributes for both serves side Model and for client side.
This is dirty way ...

Just create in doom so small that is not visible (NOT HIDDEN! ) input with same dom name.

   <span asp-validation-for="ImageRaw" style="padding-left:15px" class="text-danger"></span>
                        <input style="width:0px;border:0px!important;padding:0px;" type="text" asp-for="ImageRaw" />

If you hide it JQuery validation will ignore it. Through JQ apply changes to real input on the hidden one.

  $(document).on('change', 'input[name="PersonImage"]', function () {
            $('input#ImageRaw').val($(this).val());
        });