问题描述:

This is my asp.net markup:

<asp:TextBox runat="server" Style="width: 100px;" ID="txtCon" OnTextChanged="txtCon_TextChanged" MaxLength="10" AutoPostBack="true" />

<asp:RegularExpressionValidator ValidationGroup="con" runat="server" ControlToValidate="txtCon"

Display="Dynamic" ErrorMessage="Invalid Format. Ex. 12 is valid format" ValidationExpression="(\d){1,10}" />

Problem is if I type in abc in textbox and tab out of the control it still does a postback. I want to stop this behavior. How can I do so?

网友答案:

Do not use OnTextChanged event instead use javascript function for eg keyup let me know if this helps

<asp:TextBox runat="server"  Style="width: 100px;" ID="txtCon"   MaxLength="10" AutoPostBack="false" /> <asp:RegularExpressionValidator ValidationGroup="con" runat="server" ControlToValidate="txtCon"
                            Display="Dynamic" ErrorMessage="Invalid Format. Ex. 12 is valid format" ValidationExpression="(\d){1,10}" />

js function

 $(document).ready(function () {
            $("txtCon").each(function () {
                $(this).keyup(function () {
                   //your function here
                });
            });

        });
网友答案:

add ValidationGroup="con" AND CausesValidation="true" in text box

replace your text box with this

    <asp:TextBox runat="server" Style="width: 100px;" ID="txtCon" OnTextChanged="txtCon_TextChanged"
    MaxLength="10" AutoPostBack="true" ValidationGroup="con" CausesValidation="true"/>
相关阅读:
Top