required field validator for at least one textbox contains text

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


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I would use a CustomFieldValidator like this:

<asp:CustomValidator runat="server"
         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;
         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.

Method 2

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.

    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;

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" 
         ErrorMessage="have to fill at least 1 field" 
         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.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x