Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable client-side validation in MVC 3 "cancel" submit button

OK, been trying things for hours and could use some help. I'm trying to implement a page in MVC 3 that has "back" and "next" buttons. When the back button is clicked I want to disable client-side MVC validation from running so that my action method will run and send the user to the previous logical web page. I've tried this:

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

and this:

<input type="submit" name="backButton" value="← Back"   title="Go back to step 1." disableValidation="true" /> 

But no matter what, the cilent-side validation JavaScript kicks in and won't let the button do its post-back. I'm thinking that disableValidation only works in MVC 2 perhaps, and I'm supposed to be doing something else in MVC 3, but cannot seem to find any examples.

like image 563
Glenn Doten Avatar asked Mar 19 '11 17:03

Glenn Doten


People also ask

How do you disable client side validation?

To disable client-side validation, set the page's ClientTarget property to 'Downlevel' ('Uplevel' forces client-side validation). Alternatively, you can set an individual validator control's EnableClientScript property to 'false' to disable client-side validation for that specific control.

How do you remove data annotation validation on client side?

To disable client side validation, we need to disable it by force. Notice the @data_val= “false”. It will disable the validation on this field.

Can client side validation be bypassed?

Client-side validation is executed by the client and can be easily bypassed. Client-side validation is a major design problem when it appears in web applications. It places trust in the browser, an entity that should never be trusted.

What is form validation in MVC?

It allows integer value and sets the maximum length of characters to be allowed. minimumLength: It allows integer value and sets the minimum length of characters to be allowed. ErrorMessage: ErrorMessage accepts string when validation fails on property and it shows the error message to end user.


2 Answers

What is this mystical force that causes the answer to reveal itself as soon as you post a question somewhere?

It looks like in MVC 3 you disable client-side validation on a button by adding the class "cancel" to it. So in my example:

<input type="submit" name="backButton" value="← Back"  title="Go back to step 1." class="cancel" /> 

works great. And no ID attribute is needed either. If you have an actual style class on the button, just do this:

<input type="submit" name="backButton" value="← Back"  title="Go back to step 1." class="style-name cancel" /> 
like image 177
Glenn Doten Avatar answered Oct 21 '22 12:10

Glenn Doten


I know... very old question.. Yet it was the top of the search results when I looked.

For MVC 4 (and a BUTTON) it seems (at least for me) that the answer is simply adding the formnovalidate="formnovalidate"

 <button type="submit" class="btn btn-outline-success border-0" formnovalidate="formnovalidate" name="command" value="Back" title="Back">     <span class="fas fa-arrow-left" aria-hidden="true"></span>     &nbsp;Back </button> 
like image 44
da_jokker Avatar answered Oct 21 '22 10:10

da_jokker