Stop TextBox TextChanged event when validation fails in ASP.Net

Last Reply on May 09, 2018 05:32 AM By kalpesh

Posted on May 09, 2018 05:31 AM

Sir,

I have textbox on page and I have set its OnTextChnaged property. Used regular expression for numbers only. But when I type invalid characters textbox OnTextChange event got fired even if validation is false.

I want OnTextChange event should get fire only when user statisfy the validations.

How should I avoid this?

 html

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body><center>
    <form id="form1" runat="server">
     <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox><br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox1" Display="Dynamic" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
    </form>
    </center>
</body>
</html>

 

Posted on May 09, 2018 05:32 AM

Try by adding CausesValidation="true" to Textbox like below.

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged" CausesValidation="true"></asp:TextBox>