Solved

Validate input in textbox

Posted on 2008-06-16
12
1,490 Views
Last Modified: 2012-08-13
Hi,

in a textbox, I want users to input numbers, delimited by a ' ; '
They need to input a 7digit number, then the ' ; ' and if needed, the next 7 digit number and so on
How do I do that?
0
Comment
Question by:JamesBrian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 21793371
Try something like this:
      Private Function ValidateTextBox(ByVal txt As String) As Boolean
        Try
            If txt = String.Empty Then
                Return False
            Else
                Dim str() As String = txt.Split(";"c)
                For Each line As String In str
                    If line.Length <> 7 Or Not IsNumeric(line) Then
                        Return False
                    End If
                Next
                Return True
            End If
        Catch ex As Exception
            Return False
        End Try
 
    End Function
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ValidateTextBox(Me.TextBox1.Text) Then
            Debug.WriteLine("Valid information!")
        Else
            Debug.WriteLine("Wrong information!")
        End If
    End Sub

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 21793517
Hi JamesBrian;

You can use the Validating event handler of the text box with the help of the Regular Expression class to validate the input data.

The sample code will allow the following input data
1234567;
7894561;5821476;
1237895;;5821476;...;8889996;   ' Where ... equals any number of 7 digits followed by ;
Invalid Data
123456;                    ' only 6 digits
1234567;A789456;  ' Alpha in data


Imports System.Text.RegularExpressions
 
    Private Sub TextBox1_Validating(ByVal sender As Object, _
        ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
 
        If Not (TextBox1.Text = String.Empty) Then
            If Not Regex.IsMatch(TextBox1.Text, "^(\d{7};)+$") Then
                MessageBox.Show("Input format error: Please check input data")
                e.Cancel = True
            End If
        End If
 
    End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21793575
Well the code I post it it works this way:

Valid inputs:
1234567
1234567;1234567
1234567;1234567;1234567

Invalid inputs;
1234567;
1234567;Addaaa;
123;
etc

I think it's not practical to force the user to add always the last ";" ... but may be required :)
0
Industry Leaders: 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!

 

Author Comment

by:JamesBrian
ID: 21795294
Sorry, but I forgot to mention that the 7 digits MUST be numerical...
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21795307
Have you tried my function ?
0
 

Author Comment

by:JamesBrian
ID: 21795448
@jpaulino:
No , I have'nt, but Regular Expressions seem the logical way to do this....
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21795468
That's your decision ...
0
 

Author Comment

by:JamesBrian
ID: 21799966
@fernandosoto

Hi,
this works fine, but only for 1 '7d+;"
Entering 1234567;7654321;8523695;456852 returns an error. And that is what I was looking for
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21800429
Well my solution works fine but that's you call!

I only don't understand why you want to use regex ... because it's slower or because it sounds better.
0
 

Author Comment

by:JamesBrian
ID: 21800751
because I had made up my mind to use the REGEX solution for its clean and ' much fewer lines to code ' way of coding a complex thing.Compare your solution (which works fine, I do not argue that) with the REGEX solution.
Why would a REGEX be slower ????
Apart from that...code just needs to work, not ' sound ' better.......
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 21800915
Of course that in this simple validation the speed is not a point but just for your information I have made a test with only 10000 repetitions (on a loop and using a stopwatch) using both methods. Here's the results:

(values in miliseconds)
Code: 243
Code: 244
Code: 243
Code: 241
Code: 242
Code: 243

Regex:417
Regex:425
Regex:419
Regex:421
Regex:416
Regex:421

I also use regex but for this I didn't found really necessary. But this is just my opinion :)

Good luck
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 21802403
Hi JamesBrian;

To have either a semicolon or no semicolon at the end of the list of digits change the regex pattern from this,
 "^(\d{7};)+$", to this, "^(\d{7}?)+$".

Fernando
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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