Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate two controls (CustomValidator)

Tags:

c#

asp.net

vb.net

Before submitting the form I need to test if the sum ( txtA + txtB) is greater than 100. Is it possible to do this with a CustomValidator, because I don't know if I can choose the 2 textbox in controltovalidate

<asp:TextBox ID="txtA" runat="server"></asp:TextBox>
<asp:TextBox ID="txtB" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" 
                     runat="server" 
                     ErrorMessage="CustomValidator" />

<asp:Button ID="Button1" runat="server" Text="Button" />

Thanks.

like image 585
user1187282 Avatar asked Jan 14 '23 14:01

user1187282


1 Answers

you can do as :

<asp:TextBox ID="txtA" runat="server" /> 
<asp:TextBox ID="txtB" runat="server" />
<asp:CustomValidator ID="CV1"runat="server" 
    OnServerValidate="ServerValidation" 
    ErrorMessage="Sum is less than 100" />

codebehind :

protected void ServerValidation(object source, ServerValidateEventArgs args)
{
    args.IsValid = int.Parse(txtA.Text)+ int.Parse(txtB.Text) >100;
}
like image 65
Arshad Avatar answered Jan 18 '23 19:01

Arshad