Validate at least one of two TextBox
Hi
try these examples :
<script type="text/javascript" language="javascript"> function checkInput() { var tb1 = document.getElementById('<%= TextBox1.ClientID %>'); var tb2 = document.getElementById('<%= TextBox2.ClientID %>'); if( tb1.value == "" && tb2.value == "") { alert('TextBox cannot be emplty!'); return false; } else {return true;} } </script> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return checkInput();" />
//——or
<script type="text/javascript"> function validateControls(txt1, txt2) { if (document.getElementById(txt1).value == '' && document.getElementById(txt2).value == '') { alert('Atleaset one value required.'); return false; } else return true; } </script>
and the button that submits the page or on which you want to validate the textboxes you can add an attribute like this
this.Button1.Attributes.Add("onclick", "javascript: return validateControls('" + TextBox1.ClientID + "', '" + TextBox2.ClientID + "')");
//– or using CustomValidator Control
<script type="text/javascript" language="javascript"> function checkInput(source, args) { var tb1 = document.getElementById('<%= TextBox1.ClientID %>'); var tb2 = document.getElementById('<%= TextBox2.ClientID %>'); if( tb1.value =="" && tb2.value == "") { args.IsValid =false; } else { args.IsValid =true; } } </script> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Enter at least one" ClientValidationFunction="checkInput"></asp:CustomValidator> <asp:Button ID="Button1" runat="server" Text="Submit" />
hope it helps
Good Luck
Categories: ASP.Net
Validation
Comments (0)
Trackbacks (0)
Leave a comment
Trackback
Recent Comments