Textbox with autopostback and regularexpression validator

Posted on 2009-02-21
Last Modified: 2012-05-06
I have an input textbox on my asp page that has a function on TextChanged. The textbox only allows numeric values. When I type a character in the textbox it validates, displays the error text and takes me to the VB code. How can I keep it from going to the vb code until the value in the txtbox is valid?

ASP Code:
<asp:TextBox runat="server" ID="txtASR" MaxLength="15" Width="50" CssClass="form"
                            AutoPostBack="true" CausesValidation="true"></asp:TextBox>
                            <asp:RegularExpressionValidator SetFocusOnError="true" ID="revCountry" ControlToValidate="txtASR"
                            ErrorMessage="Country " ValidationExpression="^[-]?\d*$" Display="Static" Font-Names="Verdana"
                            Font-Size="11px" Font-Bold="true" ValidationGroup="Budget" runat="server">*</asp:RegularExpressionValidator>

VB Code
Private Sub txtASR_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtASR.TextChanged

        If txtASR.Text.Length > 0 And Me.lblATR.Text.Length > 0 Then

            Me.lblAGR.Text = FormatNumber(CInt(Me.lblATR.Text) + CInt(Me.txtASR.Text), 0)


            Me.lblAGR.Text = FormatNumber((Me.txtASR.Text), 0)

        End If

End Sub

Open in new window

Question by:JessyRobinson1234
    LVL 9

    Expert Comment

    by:Ken Fayal
    I would turn autopostback off.  If you don't want a round trip then you have to use Javascript to limit the input to the textbox to numeric values.  You can use something like this:

    'In Page_Load
    txtASR.Attributes.Add("OnKeyPress", "return isNumberKey(event)")
    'Then add this javascript code to your .aspx page.
    function isNumberKey(evt)
    	var boolResult = true;
     	var charCode = (evt.which) ? evt.which : event.keyCode;
    	if (charCode > 31 && (charCode < 48 || charCode > 57))
    		boolResult = false;
    	return boolResult

    Open in new window


    Author Comment

    I have 6 textboxes on my screen that are using autopostbox. I need to have it since it has to retrieve the value to do autocalculation on the screen based on the fetched value.

    Author Comment

    I meant to say autopostback not autopostbox.
    LVL 9

    Expert Comment

    by:Ken Fayal
    I don't understand what you are asking then.  First you asked "How can I keep it from going to the vb code" which is done through the autopostback, but you say above that you need the autopostback.  

    My suggestion above would keep people from entering any values except numeric values and then the whole form "will go to the VB code" as you put it, after the values have been validated because the textboxes won't have anything other than numeric values in them.  You can get rid of your regular expression validator.

    Author Comment

    that wouldn't work for me because every time a new value gets entered into the text box, calcuations happen based on the input (for instance the sum of value a and value b gets written to a label).
    LVL 9

    Accepted Solution

    It would still work if you do those calculations in Javascript.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now