3

I'm attempting to implement jQuery validation in my .NET page (http://jqueryvalidation.org/), and everything is perfect if I use an asp:button control.

<asp:Button ID="ContinueBtn" runat="server" CssClass="button" Text="Continue" 
                            onclick="ContinueBtn_Click" />

With my javascript code inserted, it validates the form just fine, and if no validation errors occur, then it executes the code behind associated with that button.

However, when I try to use a asp:Linkbutton instead, it posts back and the javascript isn't fired (even when I add OnClientClick="return false;").

Bottom line - how can I make the linkbutton behave like the button control?

The javascript code:

$(document).ready(function () {
    $("#form1").validate({
        rules:
        {
            <%= FirstName.UniqueID %> :
            {
                required: true,
                rangelength: [3, 12]
            }
        },
    messages:
        {
            <%= FirstName.UniqueID %> :
            {
                required: "Please enter your first name",
                rangelength: "Please enter minimum 3 characters and Maximum 12 character"
            }

        }
     });
});
kogh
  • 995
  • 4
  • 17
  • 30
  • Can you show the markup for the LinkButton that isn't working for you? – Josh Darnell Mar 18 '14 at 20:44
  • Same as Button, just minus the Text attribute. So: Continue – kogh Mar 18 '14 at 20:51
  • 1
    Ah, then I think this should help you (especially "Update 2" in the question): [ASP.NET LinkButton / ImageButton and JQuery Validate?](http://stackoverflow.com/questions/649762/asp-net-linkbutton-imagebutton-and-jquery-validate) – Josh Darnell Mar 18 '14 at 20:55

1 Answers1

1

Simple is to write a click function which checks for form validity

   $("#<%= buttonID %>").click(function() {
       return $('form').valid();
   })

give it a try i hope it will work :)

Swapy
  • 55
  • 4
  • I just created a function with "return $('#form1').valid();" in it, and added "OnClientClick" returning that functions bool to my asp:LinkButton. Problem solved :) Thanks to you and jadarnel27! – kogh Mar 20 '14 at 19:53