Question : validate 2 controls with 1 validation

Hi Experts,

I have a form that has 2 controls, a textbox (used for first available date) and a CheckBox (used for ‘After two week notice’).  

What I am looking for is a way to validate these fields.  Only one needs to be filled in or checked.  Or they can both be filled in and checked but at least one needs to be done.

I believe that I need a CustomValidator and then a RequiredFieldValidator.  My problem is I have no idea how to get the ball moving.  

This is a web page in asp.net with Visual Basic.

Attached is a file of what the controls look like.

Any Ideas?
 
 
Controls on page
339412
 

Answer : validate 2 controls with 1 validation

You cannot use RequiredFieldValidator since it is only for one field. Instead use JavaScript:

<script type="text/javascript">  
function AtLeastOne_ClientValidate(source, args)  
{  
if (document.getElementById("<%= textBox1.ClientID %>").value =="" &&  
document.getElementById("<%= CheckBox1.ClientID %>").Checked == false )  
{  
args.IsValid = false;  
}  
else  
{  
args.IsValid = true;      
}  
}  
 
</script>  

and then add CustomValidator:

<asp:CustomValidator id="AtLeastOne" runat="server"    
  ErrorMessage="TextBox or CheckBox required"    
  Display="Dynamic"    
  OnServerValidate="AtLeastOne_ServerValidate"    
  ClientValidationFunction="AtLeastOne_ClientValidate" />  

and this server-side code

Sub AtLeastOne_ServerValidate
      If TextBox1.Text.Length = 0 AndAlso CheckBox1.Checked=False Then
                        MessageBox.Show("At least one of those fields is required")
                        TextBox1.Focus()
                        Exit Sub
                    End If
End Sub
Random Solutions  
 
programming4us programming4us