Can use Custom validator with client script or with server code. Here I am showing a client side validation example
the markup..
<asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static" ValidationGroup="Group1"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ClientIDMode="Static" ValidationGroup="Group1"></asp:TextBox>
<asp:Button ID="Button2" runat="server" ValidationGroup="Group1" Text="Button" />
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="TextBox1" ValidateEmptyText="true" ValidationGroup="Group1" ClientValidationFunction="validate" runat="server" ErrorMessage="Required Fields Are Blank"></asp:CustomValidator>
I have made the clientId mode to
static
in order to use ease with javascript. The Custom validator has the
ClientValidationFunction
attribute. Can use that for validation at client side. Below is the example script to allow text box both blank or both has value. If either one has value it throws error.
<script type="text/javascript" language="javascript">
function validate(source, arguments) {
text1 = document.getElementById("TextBox1");
text2 = document.getElementById("TextBox2");
if ((text1.value == "" || text2.value == "") && !(text1.value == "" && text2.value == "")) {
arguments.IsValid = false;
} else {
arguments.IsValid = true;
}
}
</script>