Compare Validator? Or Custom Validator?

629 views Asked by At

I have two ASP.NET text-boxes

Textbox 1 Textbox 2

if a user inputs anything in textbox 1 I want to make input into textbox 2 required. What would be the best method of accomplishing this?

CompareValidator? RequiredValidator? CustomValidator?

Here's my code:

            <div class="MHStransactionDateFrom">
            <asp:Label ID="lblMHSTransactionDateFrom" runat="server" Text="Transaction Date From" Width="120"></asp:Label>
            <asp:TextBox ID="ddlMHSTransactonDateFrom" runat="server" Height="10px" Width="100px" CssClass="small date"></asp:TextBox>

        <div class="MHStransactionDateTo">
            <asp:Label ID="lblMHSTransactionDateTo" runat="server" Text="Transaction Date To" style="font-size: 1.2em; color: #FFF;" Width="200"></asp:Label>
            <asp:TextBox ID="ddlMHSTransactionDateTo" runat="server" Height="10px" Width="100px" CssClass="small date"></asp:TextBox>

                        </div>  
3

There are 3 answers

0
Dotnetter On

In my opinion the perfect choice is relative to what you want to do, if the second textbox must have the same data as the first one, you should use the CompareValidtor. if the second data is required only and not needed to be the same as the first one, here you should use the RequiredValidator

1
Trucktech On

You want input into TextBox2 required when there is ANYTHING entered in TextBox1? You could do this in your code behind. Like:

 if(TextBox1.Text.Length > 0)
 {
    if(TextBox2.Text.Length == 0)
    {
       //insert error message here
    }
    else
    {
       //do work here
    }

 }
0
Shan Khan On

textbox 1 = Required textbox 2 = compared validator with textbox 1 custom can be used too. but why re inventing the wheel if control has already give you