59

I have an ASP.NET form that takes input from a user. There's a Submit button on the form and a button called Calc which does a calculation to populate a text field. The problem I'm having is that on the form I have a set of <ASP:REQUIREDFIELDVALIDATOR> validators and when the Calc button is pressed the form gets validated. I don't want the required fields to be validated when the Calc button is pressed, only the Submit button. Any way around this?

3
  • 2
    In ASP.NET Core (because of tag helpers) you can now just add the formnovalidate attribute as usual on the <button> directly. Commented Feb 16, 2017 at 0:41
  • 1
    If anyone is working with WebForms, but is using the HTML5 required property, you can use UseSubmitBehavior="false" on your button to prevent the required check. Commented Apr 3, 2018 at 19:45
  • 2
    If anyone is working with Razor Pages, you can use formnovalidate="formnovalidate" Commented Jun 1, 2020 at 17:02

9 Answers 9

99

Set the CausesValidation property to false.

Sign up to request clarification or add additional context in comments.

Comments

26
<asp:Button runat="Server" ... CausesValidation="False" />

Button.CausesValidation (If I remember correctly).

Comments

5

Try putting CausesValidation="false" as a button attribute.

Some sample code:

http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx

Comments

4

ASPX Page:

<asp:Button ID="buttonNew" runat="server" Text="New" CausesValidation="False" />

OR

CodeBehind Page: (.cs)

buttonNew.CausesValidation = false;

Check here to know more about Validated and Validating events for the controls.

Comments

3

Set the button.causesValidation to false.

this link

However, if all it is doing is calculating something based on user input then you shouldn't have it posting back at all. I would recommend using an HTML button and attach some javascript to it to do your work for you and then you won't have this problem.

Comments

1

While designing the button, you can set its property CausesValidation="false" to avoid validation on button click event. It does not allow to validation the server control and perform its click event only

Comments

1

You should use this

UseSubmitBehavior="False"

Comments

0

To disable validation in a specific control

Set the control's CausesValidation property to false.

<asp:Button id="Button3" runat="server"
  Text="Cancel" CausesValidation="False">
</asp:Button>

To disable a validation control

Set the validation controls Enabled property to false.

To disable client-side validation

Set the validation controls EnableClientScript property to false.

For More Info

Comments

0

These days (asp.net core version 8.0) just add the attribute formnovalidate to the button.

e.g.

<button asp-route-provider="MSEntraID" formnovalidate class="login-button ms-login">
    <img src="~/images/MicrosoftIcon.svg" alt="Microsoft Logo" />
    <span id="login-btn" style="display: inline-block;">Log in with Microsoft</span>
</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.