Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restrict text box entry to letters and numbers only

Status
Not open for further replies.

megmoo75

Programmer
Jun 14, 2003
40
US
Hello -

I have a text box in which I want to restrict entry to be only numbers, letters, or spaces. No other characters (punctuation, etc) is acceptable. I'd like to make it so that if the user presses a non-allowed key (e.g. a comma), nothing happens - it won't enter it and the cursor doesn't move.

Any suggestions?

Thanks in advance!
 
There are so many discussions about this on this forum. Make a keyword search for text box, numbers and letters.

 
Thanks, I found it in this thread:

thread222-601917
 
here's another one:

thread222-643028
And there are several more threads with long discussions on this subject...
 
Being sick of writing code for this ability, I wrote a small function for the purpose. It seems to work for me but if anyone has any comments (e.g. Making it faster) please let me know.

[tt]
Private Function isTextOrNum(txtTextBox As TextBox) As String
Dim CurChar As String
Dim intLoop As Integer
Dim OutStr As String
For intLoop = 1 To Len(txtTextBox)
CurChr = Mid(txtTextBox, intLoop, 1)
If IsEmpty(CurChr) = True Then Exit Function

Select Case Asc(CurChr)
Case 48 To 57, 65 To 90, 97 To 122
OutStr = OutStr & CurChr
Case Else
OutStr = OutStr
End Select
Next intLoop

isTextOrNum = OutStr
txtTextBox.SelStart = Len(txtTextBox)
End Function



Private Sub Text1_Change()
Text1.Text = isTextOrNum(Text1)
End Sub
[/tt]

If this helps someone, great. If someone helps me by making it faster, greater!


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Oh, Oh..I read just "Numbers Only"...

JAG14, yes that's how it can be done - best icalling from or in the Change event. There is another thread somewhere which covered AlphaNumeric chars only.

In thread222-662875 there is an API function to determin this, and can be called as such:

?IsCharAlphaNumeric(ASC("A"))

But...using the
Select Case Asc(CurChr)
above is all that is really needed...
 
I usually use the keypress event for this stuff, although the change event is also fine I'm sure. If you use keypress, it's important to allow tab, enter, and backspace through, since you're evaluating ascii characters.

Bob
 
Bob, as pointed out in so many of these threads, the key press event doesn't cover the Cut and Paste scenario, although with Keypress you can capture the CTRL+C and CTRL+V combos, you can't capture mouse activity.


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 

And the Shift-Ins needs to be captured in the KeyPress -(along with the CTRL+V in the KeyDown, and the mouse right-click Popup as JAG14 mentioned)
 
Interesting. I've never gone to the trouble! I'll have to look into it next time it comes up.

Thanks guys...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top