Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get my jQuery Validator Code to run a second time after a form has already been submitted?
    primarykey
    data
    text
    <p>Here is the scenario.</p> <p>A user fills out a form. He forgets some required info. My validation code indicates to the user the missing field is required. He fills out the form fully and submits. Presto it works.</p> <p>Now, the user manually deletes the required field and hits submit again. Presto it works (IT SHOUDLNT). How can I get my validation to work a second time around to prevent empty fields from being submitted? Here is my code. It only works on 1 iteration of a form submit. All subsequent submits ignore my validation.</p> <p>Thank you...</p> <pre><code>//Validation $('#CommentsForm').validate({ rules: { Author: "required", Message: "required" }, messages: { Author: "Author is required.", Message: "Comment is required." }, errorContainer: "#CommentsErrorBox", errorLabelContainer: "#CommentsErrorBox ul", wrapper: "li" }); //User Clicked Submit Button $('#CommentsForm').live('submit', function (e) { e.preventDefault(); //Prevent Browser from getting new url //Create JSON object var jsonCommentData = { ID: $('#HiddenID').val(), Author: $('#Author').val(), Message: $('#Message').val() } //Add the comment. $.ajax({ url: '/Home/_CommentsAdd', type: 'POST', data: JSON.stringify(jsonCommentData), dataType: 'html', contentType: 'application/json', //The request was a success. Repopulate the div with new result set. success: function (data) { $('#AjaxComments').html(data); $('abbr.timeago').timeago(); //update the timestamp with timeago //Change colors of message. if ($('#CommentStatus').html() == "Your Comment Has Been Added!") { $('#CommentStatus').css('color', 'GREEN'); } }, error: function (data) { alert('Fail'); } }); }); </code></pre> <p>Here is my Partial view: _Comments</p> <pre><code>@model DH.ViewModels.CommentsViewModel &lt;div id="AjaxComments"&gt; @{ &lt;input type="hidden" id="HiddenPageNumber" value="@Model.PageNumber.ToString()" /&gt; &lt;input type="hidden" id="HiddenPageCount" value="@Model.PageCount.ToString()" /&gt; //No Comments Yet if (Model.CommentStatus.Length &gt; 0) { &lt;div id="CommentStatus"&gt;@Model.CommentStatus&lt;/div&gt; } foreach (var item in Model.Comment) { &lt;div class="CommentContainer"&gt; &lt;div class="RateComment"&gt; &lt;img src="/content/images/icons/Thumbs-Up-16x16.png" alt="Thumbs Up" title="Uprate Comment" style="margin-top: 10px;" /&gt; &lt;div class="CommentRating"&gt;8&lt;/div&gt; &lt;img src="/content/images/icons/Thumbs-Down-16x16.png" alt="Thumbs Down" title="Downrate Comment" style="margin-top: 5px;" /&gt; &lt;/div&gt; &lt;div class="CommentHeader"&gt; &lt;div class="CommentAuthor"&gt;@item.Author&lt;/div&gt; &lt;div class="MessageDate"&gt; &lt;img src="/content/images/icons/Clock-16x16.png" width="16" height="16" alt="Comment Time" style="vertical-align: middle; padding-bottom: 2px;" /&gt; &lt;abbr class="timeago" title="@item.MessageDate" style="border-bottom-width: 0;"&gt;@item.MessageDate&lt;/abbr&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="CommentMessage"&gt;@item.Message&lt;/div&gt; &lt;div style="clear: both;"&gt;&lt;/div&gt; &lt;/div&gt; } } &lt;div id="Pagination"&gt; @{ //Setting up Increments of 10 Pagination links int StartPage; int EndPage; int PageNumber = Model.PageNumber; int PageCount = Model.PageCount; if (PageCount &lt; 10) { StartPage = 1; EndPage = PageCount; } else { if (PageNumber &lt; 10) { StartPage = 1; EndPage = 10; } else { StartPage = PageNumber - 5; EndPage = PageNumber + 4; if (EndPage &gt; PageCount) { EndPage = PageCount; StartPage = EndPage - 9; } } } //Display "Page of" if there are comments. if (@Model.PageCount &gt; 0) { &lt;div id="PageOf"&gt;Page @Model.PageNumber of @Model.PageCount&lt;/div&gt; } //Set up First and Prev arrow links if (Model.PageNumber &gt; 1) { &lt;a class="PaginationLink" title="Prev" href=""&gt;&lt; Prev&lt;/a&gt; } //Loop through and create the page #'s. for (var PageCounter = StartPage; PageCounter &lt;= EndPage; PageCounter++) { //Display the Page # in a Black Box if (PageCounter == PageNumber) { &lt;span class="CurrentPage"&gt;@PageNumber&lt;/span&gt; } //Create the Page # Links. else { &lt;a class="PaginationLink" title="@PageCounter" href=""&gt;@PageCounter&lt;/a&gt; } } //Set up Next and Last arrow links if (Model.PageNumber &lt; Model.PageCount) { &lt;a class="PaginationLink" title="Next" href=""&gt;Next &gt;&lt;/a&gt; } } &lt;/div&gt; &lt;div id="CommentsFormContainer"&gt; &lt;form id="CommentsForm" action=""&gt; &lt;div id="CommentsErrorBox"&gt;&lt;ul&gt;&lt;/ul&gt;&lt;/div&gt; &lt;fieldset id="CommentsFormFieldset"&gt; &lt;legend id="CommentsFormLegend" align="center"&gt;Add a Comment&lt;/legend&gt; &lt;label for="Author" class="LabelAuthorMessage"&gt;Author:&amp;nbsp;&lt;/label&gt;&lt;input type="text" id="Author" name="Author" size="30" class="CommentInputs" /&gt; &lt;label for="Message" class="LabelAuthorMessage"&gt;Comment:&amp;nbsp;&lt;/label&gt;&lt;textarea id="Message" name="Message" cols="35" rows="5" class="CommentInputs"&gt;&lt;/textarea&gt; &lt;input type="text" id="CharCount" value="0" readonly="readonly" /&gt;&amp;nbsp;&amp;nbsp; &lt;label for="CharCount" id="LabelCharCount"&gt;* Max 200 Characters&lt;/label&gt; &lt;input type="submit" id="SubmitComment" value="Submit" name="SubmitComment" style="float: left; margin-left: 50px;" /&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload