Solved

Help with controlling data entered in a Textbox

Posted on 2011-09-22
12
248 Views
Last Modified: 2012-05-12
Hi,

I have several Textboxes which data needs to be entered in a certain format.

For examplem how do I force users to enter data in the folloing format?

xx,xxxx - Numbers only, 7 characters long

and check to see if the data was enteres in the proper format. I am using VB.NET code.

Thanks,

Victor
0
Comment
Question by:vcharles
12 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 36581961
First, you can set a mask for your textbox then add handlers to ensure only numbers are entered
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36581985
Here is a handler for the KeyPress event to use for your textbox named TextBox1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) 'Handles TextBox1.KeyPress
        e.Handled = TrapKey(CStr(Asc(e.KeyChar)))
End Sub

Open in new window

This calls the following function on every keypress
Private Function TrapKey(ByVal KCode As String) As Boolean
    If (CDbl(KCode) >= 48 And CDbl(KCode) <= 57) Or {8, 46, 44, 127, 45}.Contains(CInt(KCode)) Then
        TrapKey = False
    Else
        TrapKey = True
    End If
End Function

Open in new window

0
 

Author Comment

by:vcharles
ID: 36582009
Sorry, I don't quiet follow your code. How does it force the users to enter the data in the following format. 67,8978?
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 17

Expert Comment

by:nepaluz
ID: 36582228
Like I said before, apply a mask to your textbox aka use a masked texbox and the code above only ensures that the only data allowed to be entered is numerical.

0
 

Author Comment

by:vcharles
ID: 36583491
I Just checked all the properties of my Textbox and can't find a property the mask the textbox. Are you referring to another control similar to the reguklar textbox?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36583612
be right back ....
0
 

Author Comment

by:vcharles
ID: 36589994
Help!
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 36937473
nepaluz was referring to windows forms.net, not asp.net. There are many components out there which do the same thing for asp.net though, 3 follow... does this help?

http://www.codeproject.com/KB/edit/MaskTextBox.aspx
http://www.codeproject.com/KB/aspnet/Xtended_TextBox.aspx?q=masked+textbox+asp.net
http://www.codeproject.com/KB/aspnet/MaskingText_for_ASPNET.aspx?q=masked+textbox+asp.net

I haven't used one myself so I can't comment on specifics.
0
 
LVL 12

Accepted Solution

by:
CmdoProg2 earned 500 total points
ID: 36948622
The masked edit extender from the ajax controls toolkit is an option.  You can also try using a standard regular expression validator:

 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" 
            Display="Dynamic" ErrorMessage="Required"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
            ErrorMessage="Invalid Format (xx,xxx)" ValidationExpression="\d{2},\d{3}" ControlToValidate="TextBox1" 
            Display="Dynamic"></asp:RegularExpressionValidator>
        <asp:Button ID="Button1" runat="server" Text="Button" />

Open in new window


Or in the code behind, use a Regex object:

Dim rx As New Regex("\d{2},\d{3}")
    If rx.IsMatch(TextBox1.Text) Then
      ' place code here when in correct formae
    End If

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 36948687
Thank You All for all your Help!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

803 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