Stop TextBox TextChanged event when validation fails in ASP.Net

on May 09, 2018 05:31 AM


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 xmlns="">
<head runat="server">
    <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>


Download FREE API for Word, Excel and PDF in ASP.Net: Download
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>