Click here to Skip to main content
11,720,324 members (80,692 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 18-Mar-11 7:00am
thowra2.5K
Comments
AlbinAbel at 18-Mar-11 13:37pm
   
You can use a validation group with required field validator
AlbinAbel at 18-Mar-11 13:47pm
   
ok understand simple grouping may not work, there is a condition. Can use javascripts
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
  Permalink  
Comments
thowra at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
v2
Comments
thowra at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,029
1 OriginalGriff 844
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 21 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100