|
-
Jul 15th, 2003, 12:12 AM
#1
Thread Starter
Hyperactive Member
is letter or digit - textbox characters..
how can i tell if a user entered in ONLY a letter a-z and or a digit...
ive tried..
Dim mytext As Char
If mytext.IsLetterOrDigit(txtDistrictDB.Text) = True Then
MsgBox("yes")
Else
MsgBox("no")
End If
but if i type in a character such as !@#$%^&*()_ it returns true also.....
it doesn't work if i try to get the characters in a text box, but if i type in one character than it works.. is there a way around it rather than looping through each character?
-
Jul 15th, 2003, 12:29 AM
#2
Frenzied Member
Do you want it to be checked one by one as the user enteres the letters? If so check for the ascii of character to be in range of a-Z (if u like to include capitals too) and also 0-9. But you may also like to trap Space, Back Space, delete...
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jul 15th, 2003, 12:37 AM
#3
Thread Starter
Hyperactive Member
asci range
do you know what that asci range will be?
-
Jul 15th, 2003, 01:08 AM
#4
Frenzied Member
a-z 97-122
A-Z is 65-90
0-9 is 48-57
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jul 15th, 2003, 01:24 AM
#5
You could use RegularExpressions:
VB Code:
'search for anything other than a-zA-z0-9
If DirectCast(Regex.Matches(TextBox1.Text, "\W"), MatchCollection).Count > 0 Then
MsgBox("Contains non alphanumeric chars")
Else
MsgBox("Contains only alphanumeric chars")
End If
You'll need to import the regularexpressions namespace:
Imports System.Text.RegularExpressions
Last edited by Edneeis; Jul 15th, 2003 at 01:33 AM.
-
Jul 15th, 2003, 05:12 AM
#6
Frenzied Member
Originally posted by Edneeis
You could use RegularExpressions:
VB Code:
'search for anything other than a-zA-z0-9
If DirectCast(Regex.Matches(TextBox1.Text, "\W"), MatchCollection).Count > 0 Then
MsgBox("Contains non alphanumeric chars")
Else
MsgBox("Contains only alphanumeric chars")
End If
You'll need to import the regularexpressions namespace:
Imports System.Text.RegularExpressions
Wouldn't that cause a performance problem if he want to check it real time-letter by letter- as Regex checks for the entire text each time?
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Jul 15th, 2003, 07:39 AM
#7
Sleep mode
Originally posted by Lunatic3
Wouldn't that cause a performance problem if he want to check it real time-letter by letter- as Regex checks for the entire text each time?
I agree .
Here is how it should be done :
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case Asc(0) To Asc(9)
MessageBox.Show("You entered number : " & e.KeyChar.ToString)
Case (97) To (122)
MessageBox.Show("You entered lowercase letter : " & e.KeyChar.ToString)
Case (65) To (90)
MessageBox.Show("You entered uppercase letter : " & e.KeyChar.ToString)
End Select
End Sub
End Class
-
Jul 15th, 2003, 09:53 AM
#8
RegularExpressions should be pretty fast but if he is checking letter by letter than it might. I must have misread I thought he didn't want to check letter by letter but instead the whole text at once.
is there a way around it rather than looping through each character?
But whatever works, its all good.
-
Jul 15th, 2003, 10:10 AM
#9
all you need to do is this , to check if it's a letter / number or if it's another character :
VB Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar.IsLetterOrDigit(e.KeyChar) Then
MessageBox.Show("is a letter or number")
Else
MessageBox.Show("is a character , not a number or letter!")
End If
End Sub
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|