Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Hi,

I've got two field, one's a numeric value and the other one is a date.

What I want is to validate that both fields are either empty or filled in or in other words, if either field has a value, the other field also must have a value. The logic equivalent is "AND".

Can anyone suggest the best way to do it via client validation?

I did try custom validators on both but I don't think I can validate on the other field very easily or is there a way round this?

Thanks!
Posted
Comments
Albin Abel 18-Mar-11 13:37pm    
You can use a validation group with required field validator
Albin Abel 18-Mar-11 13:47pm    
ok understand simple grouping may not work, there is a condition. Can use javascripts

Can use Custom validator with client script or with server code. Here I am showing a client side validation example

the markup..

XML
<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.

C#
<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>
 
Share this answer
 
Comments
thowra 21-Mar-11 7:42am    
One issue I do have is that my first textbox is a date which is normally populated by the user clicking on a button and then selecting a date from the resulting calendar control. I've tried placing a regular expression validator on this first textbox (my "linked", custom validator validates the second textbox) and entering an invalid date then causes the JavaScript to fail.

I have tried setting my date textbox to read-only to ensure I get a valid date but that then means the user can't clear the textbox if they change their mind!
Just a quick update...

I'm not using ASP.NET 4.0 so couldn't use ClientIDMode="Static".

I had to make the following change:
text1 = document.getElementById("<%=TextBox1.ClientID%>").value;
text2 = document.getElementById("<%=TextBox2.ClientID%>").value;

One-liner for the return value as well (in JavaScript, empty strings are "falsy")
arguments.IsValid = (text1 && text2) || (!text1 && !text2);
 
Share this answer
 
v2
Comments
thowra 21-Mar-11 7:43am    
Nice one Jonathan! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900