5

I have an ASP.NET form for currency exchange requests. There are two text fields there: amount-source and amount-target.

One of them must be filled and only one.

How to implement this using Validators, if applicable?

abatishchev
  • 98,240
  • 88
  • 296
  • 433

2 Answers2

14

You can use Custom Validators for this:

<asp:Textbox id="textbox1" runat="server" />
<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textbox1"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage="*You can only enter 1" display="dynamic">*
</asp:CustomValidator>

<asp:Textbox id="textbox2" runat="server" />
<asp:CustomValidator id="valCustom2" runat="server"
    ControlToValidate="textbox2"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage="*You can only enter 1" display="dynamic">*
</asp:CustomValidator>

<script language="Javascript">
  function ClientValidate(source, arguments)
  {
    var tb1 = document.getElementById("<%=textbox1.ClientID %>").value;
    var tb2 = document.getElementById("<%=textbox2.ClientID %>").value;
    if (tb1 && tb2 || (!tb1 && !tb2)){
        arguments.IsValid = false;
    } else {
        arguments.IsValid = true;
    }
  }
</script>

Server-side:

protected void ServerValidate(object sender, ServerValidateEventArgs args)
{
  if(string.isNullOrEmpty(textbox1.Text) && string.isNullOrEmpty(textbox2.Text))
    args.IsValid = false;
  else if(!string.isNullOrEmpty(textbox1.Text) && !string.isNullOrEmpty(textbox2.Text))
    args.IsValid = false;
  else
    args.IsValid = true;
}

If you're using jQuery please comment...this can all be much cleaner.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    Just beware if copying the example code - "dispaly" should be "display" (occurs twice) – DJDave Nov 01 '13 at 11:39
  • I find this doesn't work if both are empty: if the text box is empty then its custom validator doesn't fire. – ChrisW Dec 18 '13 at 18:18
  • I find that to make this work (on the server side) when both are empty, I need to use a single `CustomValidator` instance without any `ControlToValidate` property. – ChrisW Dec 19 '13 at 16:59
  • 1
    @ChrisW, just set the [`ValidateEmptyText`](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.validateemptytext(v=vs.110).aspx) property to true. This happens because by default if the validator is looking at an empty field, it will skip validation altogether. – KyleMit Feb 10 '14 at 19:36
  • No need for 2 custom validators, one is enough - unless you want to fire the validations for events of both the controls, for example change event. – Ganesh Jadhav Aug 31 '15 at 08:37
  • If anyone encounters an error with this, use this link to solve your problem.https://stackoverflow.com/questions/37230036/webforms-unobtrusivevalidationmode-requires-a-scriptresourcemapping-for-jquery – Anshuman Kumar Oct 25 '19 at 05:28
1

I would implement a custom Validator and decorate both TextBoxes with it. If both are filled, then both are in a error state.

Arthur
  • 7,939
  • 3
  • 29
  • 46