Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net required field validator for at least one textbox contains text

Tags:

c#

asp.net

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?

like image 930
Shawn Avatar asked Oct 12 '10 15:10

Shawn


People also ask

Which Validator is used for value requirement in textbox?

The RegularExpressionValidator allows validating the input text by matching against a pattern of a regular expression. The regular expression is set in the ValidationExpression property.

How do you use the required field validator control?

Using the Validation Controls in ASP.NET 2.0The RequiredFieldValidator control enables you to require a user to enter a value into a form field before submitting the form. You must set two important properties when using the RequiredFieldValdiator control: ControlToValidate— The ID of the form field being validated.


2 Answers

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.

like image 97
Alex Avatar answered Oct 02 '22 05:10

Alex


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.

like image 22
lincolnk Avatar answered Oct 02 '22 03:10

lincolnk