Andrew Polidori
Andrew Polidori

Reputation: 63

Validator.IsValid is true even when value is invalid

protected void dropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    if (CompareValidatorInputTextBox1.IsValid && CompareValidatorInputTextBox2.IsValid) 
    {

        foo();
        blah();
    }
}

Hello. I am trying to take only numbers into two boxes. On the page the Validator shows up telling me that non numbers are invalid. However when I try to use the values after selecting an operation the isValid property always has true, even if the textBox validator says it's invalid. I'm new to asp.net so I'm a bit confused.

Here is one of the textboxes from my .aspx file:

<asp:TextBox ID="inputTextBox1" runat="server" />
<asp:CompareValidator 
    ID="CompareValidatorInputTextBox1" 
    runat="server"
    ControlToValidate="inputTextBox1"
    CausesValidation="True"
    operator="DataTypeCheck"
    Type="Double"
    ErrorMessage="Invalid Number"
    ForeColor="Red"
    ></asp:CompareValidator><br />

Thanks for any help you might be able to give.

Upvotes: 0

Views: 1217

Answers (1)

Arkadiusz Kałkus
Arkadiusz Kałkus

Reputation: 18371

You can force validation call by calling validate method on each validator:

protected void dropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    CompareValidatorInputTextBox1.Validate();
    CompareValidatorInputTextBox2.Validate();
    if (CompareValidatorInputTextBox1.IsValid && CompareValidatorInputTextBox2.IsValid) 
    {
        foo();
        blah();
    }
}

However in my code when I tried to reproduce your problem the validator has been called. Maybe you have some validation groups set on some controls...

Upvotes: 2

Related Questions