I need validation code in Visual Basic

Asked By 20 points N/A Posted on -
qa-featured

Hi fellow members.  I need a script for validation code in Visual Basic.  I'm trying to make a text box program with user interaction where it will react to the user if the text input is wrong.  I'm using visual Basic 6.  Thanks in advance for the help.

SHARE
Answered By 0 points N/A #145727

I need validation code in Visual Basic

qa-featured

Here is an interesting example for client side validation:

1) If you only wish to validate numeric codes, you may use the ASCII codes for range 0-9
Her Back space – 8 and 0 to 9 Integers – 48 to 57 and lastly, Delete – 127

This is how the function will look.

Private Sub Text2_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 127 Or KeyAscii = 8) Then
MsgBox "enter proper value"
KeyAscii = 0
End If
End Sub

Double click on the text box which you want to validate. This is the function for accepting input as integer value only from the user.

When we provide any other value other than numbers say any alphabet is typed in this text box it will immediately display the message from the msgbox.

2) Also for accepting only alphabets. ASCII codes are: Back Space – 8, Space – 32, Full Stop – 46, Delete – 127, Capital Alphabet – 65 to 90 and Small Alphabets – 97 to 122.

Function:

Private Sub Text5_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 97 And KeyAscii <= 122) Or KeyAscii = 127 or (KeyAscii >= 65 And KeyAscii <= 90) Or KeyAscii = 8 Or KeyAscii = 32 Or KeyAscii = 46) Then
MsgBox "enter only alphabets!"
KeyAscii = 0
End If
End Sub

3) Similarly for accepting alphabetical values for fields such as address.
This is how the function will look.

Private Sub Text3_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 97 And KeyAscii <= 122) Or (KeyAscii >= 65 And KeyAscii <= 90) Or KeyAscii = 8 Or KeyAscii = 32 Or KeyAscii = 46 Or KeyAscii = 44 Or KeyAscii = 127 Or (KeyAscii >= 48 And KeyAscii <= 57)) Then

MsgBox "enter proper values"
KeyAscii = 0
End If
End Sub

 

Login/Register to Answer

Related Questions