Search code examples
asp.net-mvc-2client-side-validation

Disable client validation while clicking certain buttons in a form in mvc 2


i am having an mvc 2 application in which i have a form that contains two buttons

 <% Html.EnableClientValidation(); %>
<%using(Html.BeginForm()){ %>
            <%= Html.LabelFor(m=> m.FormName) %>
            <%= Html.TextBoxFor(m=> m.FormName) %>
            <%: Html.ValidationMessageFor(m => m.FormName) %>
   <input type="submit" name="BtnSegment" value="Add" class="button_99" title="Add" />
    <input type="submit" name="BtnSave" value="Save" class="button_99"  title="Save" />
<%} %>

Now my requirement is to disable validation when the user clicks Add button. But validate when the user clicks Save Button. thanks, suraj


Solution

  • If you are using MSAjax for client side validation you could do this:

    <input type="submit" name="BtnSegment" disableValidation="true" value="Add" class="button_99" title="Add" />
    

    Notice the disableValidation attribute on the button. Obviously that's invalid HTML Transitional and you could cheat by appending this attribute using javascript:

    document.getElementById('idOfYourButton').disableValidation = true;
    

    Still stinky but at least the validator will be happy.

    Personally I consider MSAjax library a crap. With the jquery.validate plugin (which by the way is the default client side validation mechanism in ASP.NET MVC 3) you simlpy apply the cancel class to the button:

    <input type="submit" name="BtnSegment" value="Add" class="button_99 cancel" title="Add" />