?
Solved

Textbox with autopostback and regularexpression validator

Posted on 2009-02-21
6
Medium Priority
?
1,016 Views
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)
        Else
            Me.lblAGR.Text = FormatNumber((Me.txtASR.Text), 0)
        End If
 
End Sub

Open in new window

0
Comment
Question by:JessyRobinson1234
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:Ken Fayal
ID: 23701624
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

0
 

Author Comment

by:JessyRobinson1234
ID: 23701676
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.
0
 

Author Comment

by:JessyRobinson1234
ID: 23701682
I meant to say autopostback not autopostbox.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Ken Fayal
ID: 23701728
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.
0
 

Author Comment

by:JessyRobinson1234
ID: 23894392
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).
0
 
LVL 9

Accepted Solution

by:
Ken Fayal earned 1500 total points
ID: 23894730
It would still work if you do those calculations in Javascript.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 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