Home > ASP.Net > Validate at Least one of two TextBoxes

Validate at Least one of two TextBoxes


Hi

try this example:

** using Client Side:

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

</script>
<b>Phone: </b><asp:TextBox id="Phone" runat="server"></asp:TextBox><br />
<b>Email: </b><asp:TextBox id="Email" runat="server"></asp:TextBox>
<asp:Button id="Submit" Text="Submit" runat="server" /><br />

<%-- AtLeastOneContact Custom Validator --%>
<asp:CustomValidator id="AtLeastOneContact" runat="server" 
  ErrorMessage="Phone or Email Required"
  Display="Dynamic"
  OnServerValidate="AtLeastOneContact_ServerValidate"
  ClientValidationFunction="AtLeastOneContact_ClientValidate" />

in code behind :

** using server side

protected void AtLeastOneContact_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Phone.Text!="" && Email.Text != "")
            args.IsValid = true;
        else
        {
            args.IsValid = false;
        }
    }

Good Luck

Advertisements
Categories: ASP.Net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: