Solved

User input validation VB.Net

Posted on 2007-03-28
3
805 Views
Last Modified: 2012-06-22
Hopefully from the below people can understand what i am trying to do.....

I have a masked text box array created at runtime (00:00 mask) and wish to make sure that time entered is valid. I have been racking my brains at this all day and have not come up with a solution perhaps experts here will be able to stop me from going completely crazy. I have the below code but when i run the application i am still able to enter an invalid time.

Blocksoff(z) = New MaskedTextBox
        With Blocksoff(z)
            .Left = 355
            .Top = 52 + (z * 25)
            .Height = 20
            .Width = 35
            .Mask = TMask
            .ValidatingType = GetType(System.DateTime)
        End With
        Me.Controls.Add(Blocksoff(z))

Any help with input validation would be much appreciated.

Thanks in advance
0
Comment
Question by:dr_dudd
[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
3 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 50 total points
ID: 18811478
How about in the TextChange event, validating the text and if it isn't a valid time, clear out the text, beep or whatever and tell the user?

Basically when it is all said and done you will have a string.
You can parse that string .Substring method for each piece and see if it is in range. The first 1 or 2 digits, should be between 0 and 12 or 0 and 23 for the hour depending on military time or not. the next pairs of digits must be 0 to 60.  This shouldn't be too hard to write.

Another way to do it is in the KeyPress event, each time they press a key  you decide if the input is acceptable and if not you disallow that keypress (a little more complicated to write, but still not too bad).
0
 
LVL 34

Accepted Solution

by:
Sancler earned 450 total points
ID: 18812346
The code you show is not enough, in itself, to stop invalid entries.  Validation works via the .TypeValidationCompleted event, and then you have to code within the sub for that for how to react to invalid data.  So you need two things.  One is a sub containing your "reaction code".  The other is an event handler added to you control to raise the event that will call that sub.  At its simplest you could add a sub like this

    Private Sub CheckValidation(ByVal sender As Object, ByVal e As System.Windows.Forms.TypeValidationEventArgs)
        If Not e.IsValidInput Then
            MsgBox("Not a valid time")
            e.Cancel = True
        End If
    End Sub

and then add the following line in your With block, perhaps here

            .ValidatingType = GetType(System.DateTime)
             AddHandler .TypeValidationCompleted, AddressOf CheckValidation '<<< NEW LINE ADDED

Roger
0
 
LVL 2

Author Comment

by:dr_dudd
ID: 18814344
I have split the points as both are valid solutions. I have tried both and they both work. Many thanks experts :-)

I have used the second solution and given more points as it is a more complete answer with code.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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