8

I have three textboxes on an asp.net webform, how/can I use a required field validator to ensure that at least one of them contains text?

Shawn
  • 859
  • 1
  • 14
  • 23
  • possible duplicate of [asp.net validate textbox - at least one text box must have data in](http://stackoverflow.com/questions/1443545/asp-net-validate-textbox-at-least-one-text-box-must-have-data-in) – David d C e Freitas Oct 03 '13 at 22:37

2 Answers2

16

I would use a CustomFieldValidator like this:

<asp:CustomValidator runat="server"
         ID="MyCustomValidator"
         ValidationGroup="YOUR_VALIDATION_GROUP_NAME"
         OnServerValidate="MyCustomValidator_ServerValidate"
         ErrorMessage="At least one textbox needs to be filled in." />

and then in your codebehind you have:

protected void MyCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
     if (/* one of three textboxes has text*/)
         args.IsValid = true;
     else
         args.IsValid = false;
}

You can also add a Client-side component to this validation, and make it sexy by extending it with AJAX toolkit's ValidatorCalloutExtender control.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Alex
  • 3,644
  • 2
  • 19
  • 27
  • I didn't like that this cause a postback so I just ended up setting the ClientIDMode="static" and hard coding the values in a JS function. I wasn't really interested in doing any error messages or anything; I just wanted the button to do nothing. Thanks for the code. It totally worked, its just using a CustomValidator was the wrong choice. Which is my fault, not yours. – Shawn Oct 13 '10 at 04:01
  • I found this would not trigger the validation with a ValidationGroup specified. Once I took that out, it worked like a charm. – techturtle Jun 07 '13 at 19:09
14

I don't think a RequiredFieldValidator fits your requirements. I would go with a CustomValidator assigned to any of your fields and manually check them all when it fires.

<script>
    function doCustomValidate(source, args) {

        args.IsValid = false;

        if (document.getElementById('<% =TextBox1.ClientID %>').value.length > 0) {
            args.IsValid = true;
        }
        if (document.getElementById('<% =TextBox2.ClientID %>').value.length > 0) {
            args.IsValid = true;
        }
        if (document.getElementById('<% =TextBox3.ClientID %>').value.length > 0) {
            args.IsValid = true;
        }
    }
</script>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" 
         ErrorMessage="have to fill at least 1 field" 
         ControlToValidate="TextBox1" 
         ClientValidationFunction="doCustomValidate"
         ValidateEmptyText="true" ></asp:CustomValidator><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />

Don't forget to set ValidateEmptyText="true" as the default is to skip empty fields. make sure you create a similar server-side validation method as well.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
lincolnk
  • 11,218
  • 4
  • 40
  • 61