Ad

Adding Jquery Validation To Dynamically Generated Textboxes In A Form

I am using ASP.NET MVC framework for a form in which the textboxes are dynamically generated. I want to add jquery validation for all the textboxes for numeric entry. How do I do that?

<table>

    @using (Html.BeginForm("Submit", "Expenses", FormMethod.Post, new { enctype = "multipart/form-data", id = "UploadForms" }))
    {
       @Html.AntiForgeryToken() 
    HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
    @Html.ValidationSummary(false, "", new { @class = "text-danger" })
    if (Model.ExpenseDetails != null && Model.ExpenseDetails.Count > 0)
    {
        int i = 0;
        int l_ExpHead = Model.ExpenseHeadDict.Count;
        int l_ExpDate = Model.ExpenseDateDict.Count;

            <thead class="thead-light">
                <tr>
                <th scope="col" style="width:15%">Expense Title</th>
                @for (int col = 0; col < l_ExpDate; col++)
                {
                        <th colspan="2" scope="col" style="width:15%">@Model.ExpenseDateDict[col].ToString()</th>
                }
                </tr>
            </thead>

            <tbody>

                @for (int row = 0; row < l_ExpHead; row++)
                {
                <tr>
                    <th scope="row">@Model.ExpenseHeadDict[row].ToString()</th>

                    @for (int col = 0; col < l_ExpDate; col++)
                    {
                        @Html.HiddenFor(model => model.ExpenseDetails[i].ExpenseHead)
                        @Html.HiddenFor(model => model.ExpenseDetails[i].DateOfExpense)
                        string l_UploadID = "Upload_" + i;
                        string l_AmountID = "Amount_" + i;
                        string l_Span = "Span_" + i;
                        <td>

                            @Html.TextBoxFor(o => o.ExpenseDetails[i].Amount, new { @id = @l_AmountID, @class = "form-control input-group", @style = "width:140px" , @placeholder = "Amount"})
                            @Html.ValidationMessageFor(o => o.ExpenseDetails[i].Amount, "", new { @class = "text-danger" })                         

                        </td>
                         <td>   
                             <span class="fa fa-plus input-group" onclick="document.getElementById('@l_UploadID').click(); ShowFileCountLabel('@l_UploadID','')"></span>
                        </td>
                        i++;
                    }
                </tr>


                }

            </tbody>


        }
    }
</table>

<script>
$(document).ready(function () {
 $("#UploadForms").validate();
        $("input").each(function () {
            $(this).rules("add", {
                number: true,
                messages: {
                    number: "Please enter a valid amount."
                }
            });
        });

    })
</script>

Expected result: On submit button click, I want the validation message for the textboxes to be displayed if the user enters any non-numeric entry.

Ad

Answer

Just set type="number"

 @Html.TextBoxFor(o => o.ExpenseDetails[i].Amount, new { @id = @l_AmountID, @class = "form-control input-group", @style = "width:140px" , @placeholder = "Amount",@type="number"})
Ad
source: stackoverflow.com
Ad