Solved

User input validation VB.Net

Posted on 2007-03-28
3
790 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
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now