This topic will demonstrate how to perform client side validations in Visual Basic 6.0. It will guide you with a step by step procedure.
1)
Firstly in the below image suppose you want to accept only integer numbers as id in the textbox next to ID label in the form from the user you can perform client side validations in the following way:
These are the ascii codes that i am using for acepting only integer values from user.
Back space - 8
0 to 9 Integers - 48 to 57
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.
Next select the event as keypress from top right list box. In this case the name of text box is text1.
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)
Similarly for accepting only alphabetical values.
These are the ascii codes that i am using for acepting only name from user in the textbox for name input.
Back Space - 8
Space - 32
Full Stop - 46
Capital Alphabet - 65 to 90
Small Alphabet - 97 to 122
Delete - 127
This is how the function will look.
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.
These are the ascii codes that i am using for acepting address from user in the textbox for address input.
Back Space - 8
Space - 32
Comma - 44
Full Stop - 46
0 to 9 Integers - 48 to 57
Capital Alphabet - 65 to 90
Small Alphabet - 97 to 122
Delete - 127
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
4)
I you are making entry into a database for an employee and his joining date should be before todays date then it could be like this.
This is the code for that.
If CDate(Text3.Text) >= Date Then
MsgBox "hire date must be in the past!"
Text3.SetFocus
1)
Firstly in the below image suppose you want to accept only integer numbers as id in the textbox next to ID label in the form from the user you can perform client side validations in the following way:
These are the ascii codes that i am using for acepting only integer values from user.
Back space - 8
0 to 9 Integers - 48 to 57
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.
Next select the event as keypress from top right list box. In this case the name of text box is text1.
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)
Similarly for accepting only alphabetical values.
These are the ascii codes that i am using for acepting only name from user in the textbox for name input.
Back Space - 8
Space - 32
Full Stop - 46
Capital Alphabet - 65 to 90
Small Alphabet - 97 to 122
Delete - 127
This is how the function will look.
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.
These are the ascii codes that i am using for acepting address from user in the textbox for address input.
Back Space - 8
Space - 32
Comma - 44
Full Stop - 46
0 to 9 Integers - 48 to 57
Capital Alphabet - 65 to 90
Small Alphabet - 97 to 122
Delete - 127
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
4)
I you are making entry into a database for an employee and his joining date should be before todays date then it could be like this.
This is the code for that.
If CDate(Text3.Text) >= Date Then
MsgBox "hire date must be in the past!"
Text3.SetFocus
hi i need to check the input is alphabet or not
ReplyDeleterefer point 2 and 3 for all the details you want!
ReplyDeletehttp://niteshpatare.blogspot.in/2010/11/validations-in-visual-basic-60.html
hii
ReplyDeletethis blog is very useful for my project
hii
ReplyDeletethis blog is very useful for my project
hey i need to limit the number of entries to enter an phone no.......
ReplyDeleteThis Blog is very UseFull nd if admin can help me i will be very thankfull to Him\Her
ReplyDeleteMy Email is
shahzeb_sharif@hotmail.com
this is really important for project...
ReplyDeletecan you also give validations for e-mail address
ReplyDeletemy email is-
jainmridul12@gmail.com
Thank u this is useful
ReplyDeletePlease can u provide validation for e-mail
ReplyDeletehow to check error for 2nd condition?
ReplyDelete