Textbox with autopostback and regularexpression validator

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

Who is Participating?
Ken FayalConnect With a Mentor CTOCommented:
It would still work if you do those calculations in Javascript.
Ken FayalCTOCommented:
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

JessyRobinson1234Author Commented:
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.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

JessyRobinson1234Author Commented:
I meant to say autopostback not autopostbox.
Ken FayalCTOCommented:
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.
JessyRobinson1234Author Commented:
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).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.