Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:34:49+00:00 2026-06-16T16:34:49+00:00

I am (finally) diving into JQuery and was wondering how to correctly handle validation

  • 0

I am (finally) diving into JQuery and was wondering how to correctly handle validation of text inputs before posting. I have an ASPX page that will loop over a part of my model calling an User Control for each loop. I also have a JQuery Add link that will add a new (blank) User Control to the collection.

The page looks like:

    <h1>Create Document List</h1>

    <% Html.EnableClientValidation(); %>

    <% using (Html.BeginForm("CreateList", "Documents", FormMethod.Post, new { @class = "list", @enctype = "multipart/form-data" })) 
    { %>
        <%= Html.AntiForgeryToken() %>

        <div id="editorRows">
            <% foreach(var q in ViewData.Model.DocumentAndFileList)
                   Html.RenderPartial("DocEditRow", q);
            %>
        </div>
        <br />

        <%= Html.ActionLink("Add another...", "Add", null, new { id = "addItem" }) %>

        <br />
        <br />
        <br />

        <%= Html.SubmitButton("btnSave", "Save Document List")%>
        <%= Html.Button("btnCancel", "Cancel", HtmlButtonType.Button, "window.location.href = '" + Html.BuildUrlFromExpressionForAreas<DocumentsController>(c => c.Index()) + "';") %>
    <% } %>
</asp:Content>

The User Control looks like:

<div class="editorRow">
    <% using (Html.BeginCollectionItem("docs"))
       { %>

        <%= Html.Hidden("Document.Id", (Model != null) ? Model.Id : 0)%>

        <label for="Number">Document Name:</label>
        <%= Html.TextBox("Number", (Model != null) ? Model.Number : "", new { @size = "50", @maxlength = "255" })%>
        <%= Html.ValidationMessageFor(m => m.Number) %>

        &nbsp;

        <% if (Model != null && Model.FileName != null && Model.FileName.Length > 0)
           { %>
            <label>Current File:</label>
            <%= Model.FileName%>
        <% }
           else
           { %>
            <label>
                File Upload:
                <%= Html.FileBoxFor(m => m.HttpPostedFileBase)%>
            </label>
        <% } %>
        <a href="#" class="deleteRow">delete</a>
    <% } %>
</div>

I followed Scott Gu’s advice on an old post of his on his blog. For some reason, I am not getting any validation. The previous devs on this project used a lot of JQuery so I am now trying to get JQuery validation to work but don’t really know where to start. Googling seems to bring me to about 500 different ways to do this. Any pointers would be appreciated.

TIA


NOTE:

I have added the following to my page:

<asp:Content ID="myScript" ContentPlaceHolderID="pageScript" runat="server">
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            $("#LoadDocs").validate();
            $.validator.unobtrusive.parse("#LoadDocs");
        });
    </script>
</asp:Content>

This seems to be partially working. If I add several new rows to my collection, only the first row will pop the message if the textbox is empty. I also added a

class=”required”

to all of my inputs.

I am still struggling to get it to work 100% though. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:34:51+00:00Added an answer on June 16, 2026 at 4:34 pm

    I never figured out a way to use the built in validators. What I ended up doing was this:

    <asp:Content ID="myScript" ContentPlaceHolderID="pageScript" runat="server">
        <script type="text/javascript" language="javascript">
            $().ready(function () {
                $("#addItem").click(function () {
                    //alert('hi');
    
                    $.ajax({
                        url: this.href,
                        cache: false,
                        success: function (html) { $("#editorRows").append(html); }
                    });
                    return false;
                });
    
                $("a.deleteRow").live("click", function () {
                    $(this).parents("div.editorRow:first").remove();
                    return false;
                });
            });
    
            function OnDocUpload() {
                var editorRows = $('.editorRow');
                //alert(editorRows.length);
                var isGood = true;
                editorRows.each(function () {
                    var input = $(this).children('.required.input').first();
                    var msg = $(this).children('.display_message').first();
                    //alert(input.val().length + " | " + msg.html);
                    if (input.val().length < 1) {
                        isGood = false;
                        input.css('background-color', '#FEADCE');
                        msg.text("A document name is required!");
                    } else {
                        input.css('background-color', '#FAFAD2');
                        msg.text("");
                    }
                });
                return isGood;
            }
    
            function SubmitForm() {
                if (OnDocUpload()) {
                    var frm = $("#LoadDocs");
                    frm.submit();
                    //$("#LoadDocs").submit();
                }
            }
        </script>
    </asp:Content>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    
        <h1>Create Document List</h1>
    
        <% 
        Html.EnableClientValidation();
        using (Html.BeginForm("CreateList", "CIPDocuments", FormMethod.Post, new { @class = "wufoo", @enctype = "multipart/form-data", @id = "LoadDocs" })) 
        { %>
            <%= Html.AntiForgeryToken() %>
    
            <div id="editorRows">
                <% foreach(var cipDocument in ViewData.Model.CIPDocumentAndFileList)
                       Html.RenderPartial("DocEditRow", cipDocument);
                %>
            </div>
            <br />
    
            <%= Html.ActionLink("Add another...", "Add", null, new { id = "addItem" }) %>
    
            <br />
            <br />
            <br />
    
            <%= Html.Button("btnSave", "Save Document List", HtmlButtonType.Button, "SubmitForm();")%>
            <%= Html.Button("btnCancel", "Cancel", HtmlButtonType.Button, "window.location.href = '" + Html.BuildUrlFromExpressionForAreas<CIPDocumentsController>(c => c.Index()) + "';") %>
        <% } %>
    </asp:Content>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finally got maps api implemented into jquery accordion, mainly by changing var map;
I finally let windows nag me into upgrading from IE6 to 8. Now in
I finally have my C++ Builder 2010 installation the way I want it, with
I have a pig script in which I am loading a dataset, diving it
finally I got the idea how to handle touch and gestures events on Blackberry,
Finally, I completely follow this page ( connecting android apps to mysql database )
Finally I have decided to get rid of aptana from eclipse but the Software
Greetings, people. I'm diving into Objective-C for pretty much the first time. It's kicking
Finally moved to Rails 3 for a new project and already running into a
Finally, I have been able to get through reflection, a member of type Func<T,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.