Run ASP.Net Custom Validator only on click of one of multiple buttons on the page

asp.netcustomvalidator

I have the following code in my ASP.Net (4.0) page, in which there are 2 buttons – 'Button1' and 'Button2', a textbox, a required field validator and a custom validator.

I would like the custom validator to fire only when 'Button1' clicked and not when 'Button2' is clicked and Button2 still needs to evaluate a required field validator. How would I make this happen?

   Input 1:
 <asp:TextBox id="txtCustomData" runat="server"   />
  <asp:CustomValidator id="CustomValidator1"   
      runat="server" ErrorMessage="Number not divisible by 2!"
      ControlToValidate="txtCustomData" 
      ClientValidationFunction="CheckEven"  />
 <br/>
 Input 2:
 <asp:TextBox id="TextBox2" runat="server"    />
    <asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="TextBox2" 
           ErrorMessage="* Required" ForeColor="Red" > 
     </asp:RequiredFieldValidator>
    <br/>
   <br/>
    <asp:Button id="btn1" runat="server" Text="Button1" />
   <br/>
   <asp:Button id="btn2" runat="server" Text="Button2" />

 <script language="javascript">
<!--
function CheckEven(source, args) {
    var val = parseInt(args.Value, 10);
    if (isNaN(val)) {
        args.IsValid = false;
    }
    else {
        args.IsValid = ((val % 2) == 0);
    }
}
// -->
</script>

UPDATE 1:
While Rick's answer is a possible answer, I found another approach to handling this situation.

I can use Validation groups when both buttons need to validate 2 different validators and one of the validators is a custom validator. Set ValidationGroup="Group1" for Button1 that needs to evaluate the custom validator and ValidationGroup="Group2" for Button2 that needs to evaluate the required field validator, and include the same values for corresponding validators. There is no need to include ValidationGroup for the textboxes.

   Input 1:
 <asp:TextBox id="txtCustomData" runat="server"   />
  <asp:CustomValidator id="CustomValidator1"   
      runat="server" ErrorMessage="Number not divisible by 2!"
      ControlToValidate="txtCustomData" 
      ClientValidationFunction="CheckEven"  />
 <br/>
 Input 2:
 <asp:TextBox id="TextBox2" runat="server"    />
    <asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="TextBox2" 
           ErrorMessage="* Required" ForeColor="Red" ValidationGroup="Group2"> 
     </asp:RequiredFieldValidator>
    <br/>
   <br/>
    <asp:Button id="btn1" runat="server" Text="Button1" ValidationGroup="Group1"/>
   <br/>
   <asp:Button id="btn2" runat="server" Text="Button2" ValidationGroup="Group2"/>

 <script language="javascript">
<!--
function CheckEven(source, args) {
    var val = parseInt(args.Value, 10);
    if (isNaN(val)) {
        args.IsValid = false;
    }
    else {
        args.IsValid = ((val % 2) == 0);
    }
}
// -->
</script>

UPDATE 2:
If you end up using custom javascript in OnClientClick event of the button, then you need to be careful with your javascript else you might end up having your button never postback even when data is valid.

For example if you have written a custom javascript function called 'ValidateData( )' then first make sure that it always has return true or return false statement in it, and second your OnClientClick should use this function in the manner shown below. Most developers will simply use OnClientClick = "return ValidateData();" which will make the ASP.Net button to NEVER perform an ASP.Net post back even when the data is evaluated as valid, since the default __doPostBack JavaScript function in ASP.Net will never get called (assuming UseSubmitBehavior="false" for this button, if UseSubmitBehavior="true" then __doPostBack is not used and button will postback).

   <asp:Button id="btn1" runat="server" Text ="Button1" 
        OnClientClick="var r = ValidateData(); if (!r) { return false; } " />
  <script type=<script type="text/javascript">
  function ValidateData( )
     {
       var x = document.getElementById('xy1');
        if(x.value == '1')
         {
           return false;
         }

        return true;
      }
    </script>

Best Solution

Include the CausesValidation attribute and set it to False. Your button will not trigger validation.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation(v=vs.110).aspx

<asp:Button id="btn2" runat="server" Text="Button2" CausesValidation="False"/>
Related Question