Validate Radio Button Javascript + MVC Razor

Asked

Viewed 345 times

1

Need to validate if all my Radio have been checked, can you help me perform this validation on JS? My cshtml is mounting the radio’s through a Model List<> .

<div id="checklists" class="row">
        <div class="col-md-6">
            @{int index = 0; }
            @foreach (var descricao in @Model.Domiciliacao.listaPerg)
            {
                <div class="descricaoItemChecklist" style="height: 40px;">
                    <table class="table table-striped" width="100%" id="tbListaPerg">
                        <tbody>
                            <tr>
                                <td width="80%">@descricao.descperg</td>
                                <td width="10%">
                                    <label class="CheckBox-inline">
                                        @Html.RadioButtonFor(m => m.Domiciliacao.listaPerg[index].ckPerg, true, new { @class = "radio-inline", @id = "rbListaPerg" }) Sim
                                    </label>
                                    @Html.HiddenFor(m => m.Domiciliacao.listaPerg[index].codperg)
                                    @Html.HiddenFor(m => m.Domiciliacao.listaPerg[index].descperg)
                                </td>
                                <td width="10%">
                                    <label class="CheckBox-inline">
                                        @Html.RadioButtonFor(m => (m.Domiciliacao.listaPerg[index].ckPerg), false, new { @class = "radio-inline", @id = "rbListaPerg" }) Não
                                    </label>
                                    @Html.HiddenFor(m => m.Domiciliacao.listaPerg[index].codperg)
                                    @Html.HiddenFor(m => m.Domiciliacao.listaPerg[index].descperg)
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>  
                index = index + 1;
            }
        </div>
    </div>

1 answer

0

I was able to resolve by forcing the yes check and creating the validation for Yes or No.

 @Html.RadioButtonFor(m => m.Domiciliacao.listaPerg[index].ckPerg, true, new { @class = "radio-inline", @checked = true}) Sim

var check = true;
        $(".validar:input").each(function () {
            if ($(this).prop("type") == "radio") {
                if ($(this).attr("checked") == "True") {
                    if ($(this).val() == "N") {
                        check = false;
                    }
                }
            }
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.