Allow only character value

rvkumaar
04-10-2003, 12:35 PM
Hi
I have a text box in VB and i should allow the user to enter characters only.
Please let me know how to do it.
Thank you
kumarrv

Irotallic
04-10-2003, 12:52 PM
Use the function IsNumeric to check if a number is entered.

So your code should be somthing like this:

If Not IsNumeric(Text1.Text) Then
Ask again if you don't understand what I am saying.

Squirm
04-10-2003, 12:58 PM
You can use the KeyPress event:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 47 And KeyAscii < 58 Then KeyAscii = 0
End Sub

rvkumaar
04-10-2003, 01:08 PM
Hi
I want the user to enter characters only, ie, they can enter
lara
jack.
I should throw message if they enter numbers or wild card characters, ie, they should not enter 1,2,... or +,-,*.
I hope im clear now.
thanks
kumaarrv

Irotallic
04-10-2003, 01:21 PM
You can filter out all the symbols you wan't if you use the code squirm posted. Just look into an ascii tabel what you wan't to filter and add it into the line of code.

If (KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii > .. And KeyAscii < ..) Then KeyAscii = 0

Squirm
04-10-2003, 01:56 PM
If your filter gets a little more complex, I suggest the following method:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Const sDontAllow = "0123456789-+" 'Set to all the chars you dont allow
If InStr(1, sDontAllow, Chr$(KeyAscii)) Then KeyAscii = 0
End Sub

If your 'not allowed' is more than your 'allowed' then you can switch it around, having a constant which contains all the allowed characters, and searching that. If not found, dont allow the character.

Irotallic
04-11-2003, 02:09 AM
That's a pretty smart solution squirm. I would remeber that one. It's so simple coded, but is has a great effect :)

rvkumaar
04-11-2003, 09:53 AM
Hi
Thanks Squirm and Irotallic.
rvkumaar

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum