DFBarracks - Forums

Delta Force Barracks Forums - Powered by XMB 1.9.11
Delta Force Barracks Forums
Not logged in [Login - Register]
Go To Bottom

Printable Version  
Author: Subject: Tutorial level: Beginners: String Validation
Distant Storm


Posts: 870
Registered: 9-13-2004
Member Is Offline

Mood: Distant

[*] posted on 6-28-2007 at 11:16 PM
Tutorial level: Beginners: String Validation


Ok in this tutorial we are going to master abit of validation of user input. This stops any characters you do not want to be put into your program.

For this im going to use a simple concept just through the form. I will then post another tutorial on how to make the validation into a module. This will allow you to use it in any of your projects and also validate strings very easily.

Step 1:

Firstly create a new project by opening VB and selecting standard exe project.

Step 2:

Now we need to make a textbox for the user to input into your system.

To do this click the textbox object in the left hand control paneland then drag it out on the form window.

After you have dragged out the textbox, clear the text already set in it. Do this by the properties box on the right, change the text option to nothing.

Step 3:

Now were going to stop the user from entering letters, so that they can only enter a number.

firstly bring up the code window by pressing the switch to code buttn showin the image above.

Next select the drop down menu on the left and change it to the text1 or the textbox name if you have decided to rename it.

This will bring up a sub routine which states on change then run the subroutine.

We do not want this, we want it so when the user presses a key on the textbox it runs the sub routine.

So you must next change the textbox on the right to on KEYDOWN as shown below

You will now be given something that looks like this

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) End Sub

This states that on keydown then the sub routine is run. Also notice that a parameter called keydown is passed to it. This holds the ASCII code of the key that the user has pressed.

Step 4:

You now get into abit of code, we need to state options for which keys are which.

We need to allow numbers 0 to 9 which in ASCII values are ...

48 - 57 (inclusive)

so the next line of code we use an if statment

so inbetween the lines above type

If (KeyCode < 48) Or (KeyCode > 57) Then MsgBox ("You cannot enter this character") Text1.Text = Empty End If

The first line of code determins if the key pressed is below 48 ( NOT A NUMBER) or above 57 (NOT A NUMBER) then

next line dispalys a message box that will pop up. This will contain the details in speach marks.

After that you clear the textbox to make sure the number is not put in.

We then have to put end if lines in as the whole statment is not on one line. So you need to tell VB where it ends

The full thing should look like this

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode < 48) Or (KeyCode > 57) Then
MsgBox ("You cannot enter this character")
Text1.Text = Empty
End If
End Sub

Next run the program by clicking the play button or F5 or Run then Play button. :)

End of tutorial

For further development in other tutorials we shall look at the following addons and expantions

- Deleting only the last character input into the textbox and not the whole text

- Setting many different data types for input.

- validating the length of the string.

- highlighting textboxes with errors.

- pattern matching.

- case sensitivity

- developing the validation into a module for re-use again and again.

Kind regards

Distant Storm

View user's profile Visit user's homepage View All Posts By User
bullet magnet


Posts: 263
Registered: 6-26-2005
Member Is Offline

Mood: Modding

[*] posted on 12-23-2007 at 01:10 PM

Just got VB6!

Good tutorial.

Im gonna start learning VB6 now aside from learning VB 2005 Express Edition.

View user's profile View All Posts By User

  Go To Top

Powered by XMB 1.9.11
XMB Forum Software © 2001-2012 The XMB Group
[Queries: 15] [PHP: 57.5% - SQL: 42.5%]

Copyright © 2002 - 2008 All Rights Reserved - DFBarracks.com
Site Design by A.Bullet - ScreamingDemon.com