Share via


Disabling client side validation on submit button?

Question

Tuesday, February 9, 2010 3:59 PM

Greetings:

For the release of Microsoft MVC 2.0 RC2, can someone go into further detail on the following BUG/FIX:

You can now programmatically disable client validation for a submit button (an input elements whose type attribute is set to “submit”) even if the button does not have its name attribute set, whereas before it required the name attribute.

Does this apply to the HTML Helper Submit Button or the input submit button?        

How would I disable client validation for a submit button?  I tried using the htmlAttribute causesvalidation="false" on a HTML Helper Submit button and it did not work.

Please advise.

Thank you for time.

Barbara

 

All replies (12)

Thursday, February 11, 2010 12:44 PM ✅Answered

Don't use the Html.SubmitButton() helper.  Use <input type="submit" ... /> instead.  Make sure it has id="btnSubmit".


Tuesday, February 9, 2010 8:37 PM

Try removing the html helper Html.EnableClientValidation.


Wednesday, February 10, 2010 12:38 AM

causesvalidation="false"

This is not Web Form's Server Control.

Add htmlAttribute onclick="this.form.submit();"


Wednesday, February 10, 2010 3:15 PM

Hi malcolms:

I have a wizard with 3 submit buttons.  I want to disable validation for only one of the 3 buttons, the "Previous"  to allow the user to navigate back to the previous page without validation. 

Please let me know if you have any other suggestions.

Thanks


Wednesday, February 10, 2010 3:47 PM

Validation will not take place when the submit button someButton is clicked if the DOM element corresponding to *someButton *has a field "disableValidation" (note capitalization) whose value is true.

In practice, what this means is that you'd have a Javascript snippet placed after your button:

<script type="text/javascript">
  document.getElementById("someButton").disableValidation = true;
</script>

This snippet will disable validation on the target button.


Wednesday, February 10, 2010 4:14 PM

Hi levib:

By setting the htmlAttribute disableValidation="true" for someButton, shouldn't MVC handle the rest internally i.e. generate needed javascript:

<script type="text/javascript">
  document.getElementById("someButton").disableValidation = true;
</script>

 From the Release Notes for MVC 2 RC2

1)  You can now programmatically disable client validation for a submit button (an input elements whose type attribute is set to “submit”) even if the button does not have its name attribute set, whereas before it required the name attribute.

and from the Release Notes for MVC 2 RC

2)  Buttons in a form can specify that they do not cause validation logic to run. The default is that every button in a form causes validation logic to run, and if validation fails, the validation logic blocks submission of the form. Enabling validation selectively for buttons lets you create forms that can post information even if a form is not complete or if the form contains data that is temporarily invalid.

where these items documented?  I am unable to find it in MSDN in documentation.

 

Thank you for your time.

 

Barbara

 


Wednesday, February 10, 2010 4:58 PM

By setting the htmlAttribute disableValidation="true" for someButton, shouldn't MVC handle the rest internally

It's not an HTML attribute, it's a field on the DOM element corresponding to the button.  This must be set by user code.  Otherwise we would have forced you to write non-validating XHTML, which is bad.  We may make this better in V3, but this is what we have at the moment.

where these items documented?  I am unable to find it in MSDN in documentation.

I think MSDN documentation for RC was finished before this feature went in.  It should be updated for RTM.


Thursday, February 11, 2010 12:02 PM

Hi levib:

 

I put the snippit of javascript inside, see below and client side validation did not disable for that button:

 

<%= Html.SubmitButton("btnSubmit", "Previous", new { @class = "clPrevBtn" })%>
<script type="text/javascript">
         document.getElementById("btnSubmit").disableValidation = true;
</script>

Client-side validation is enabled:

<% Html.EnableClientValidation(); %> 

 

<%=Html.TextBoxFor(f => f.txtAIFName)%>
<%=Html.ValidationMessageFor(f => f.txtAIFName, "fsalkfjlkds;ajflksd;jf") %>

Please advise.

Thanks.


Thursday, February 11, 2010 3:42 PM

Don't use the Html.SubmitButton() helper.  Use <input type="submit" ... /> instead.  Make sure it has id="btnSubmit".

 

I switched to using an input submit button and received the same results, the button is still validating when clicked.

<input type="submit" id="btnSubmit" value="Previous" class="clPrevBtn"/>
<script type="text/javascript">
         document.getElementById("btnSubmit").disableValidation = true;
</script>

 

I am wondering why would using an input submit button verses an Html.SubmitButton() helper buttonhave different results?  Doesn't the Html.SubmitButton helper button get translated to an input submit button?

 


Thursday, February 11, 2010 11:09 PM

disableValidation should work


Thursday, March 18, 2010 9:16 AM

I was able to get it working with the suggestion from this post to add the "disableValidation=true" attribute to the input element of "type=submit".

Example:

<input id="myButton" type="submit" value="Cancel" disableValidation="true" />

**Make sure you have included the javascripts: **

<script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMVCAjax.js"></script>
<script type="text/javascript" src="../../Scripts/MicrosoftMVCValidation.js"></script>


Friday, June 24, 2011 2:57 PM

You can add a css class of cancel to a submit button to suppress the validation

e.g

<input class="cancel" type="submit" value="Save" />