how could i make pure text only when i type on a textbox? numbers must not be accept... but when i type something like this asdf123 its accept... how could i make it pure text only?
Code:
if isnumeric(text1.text) = true then
msgbox "No Numbers Allowed"
end if
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then '~~~> If it is not a number (0 - 9) Ascii value = 48 - 57
If KeyAscii <> 8 Then '~~~> Allow Backspace
KeyAscii = 0 '~~~> Make it an invalid entry
End If
End If
End Sub
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
Private Sub Command1_Click()
If IsNumeric(Text1) = True Then
MsgBox "No Numeric Allowed", vbCritical, "warning"
MsgBox "Must Be Text only",vbCritical, "warning"
Text1.Text = ""
Text1.SetFocus
Exit Sub
End If
End Sub
it allows this asdf123 when i inputted what's wrong with my code?
also allows this asdf123asdf i want to happen is only text only must be entered
Private Sub Text1_KeyPress(KeyAscii As Integer)
'~~> Ensures numbers are not keyed it
'~~> Also ensure the user is not able to paste number
If KeyAscii > 47 And KeyAscii < 58 Or _
KeyAscii = 22 Then KeyAscii = 0
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Private Sub Command1_Click()
If Not Text1.Text Like "*[!0-9]*" Then
MsgBox "Valid.. Contains only digits (0 to 9)"
Else
MsgBox "Contains something else..."
End If
End Sub
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
Here is the code for disabling paste using right mouse click... I am sure the user can take care of insert like the way I took care of Ctrl + V
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'~~> Ensures numbers are not keyed it
'~~> Also ensure the user is not able to paste number
If KeyAscii > 47 And KeyAscii < 58 Or _
KeyAscii = 22 Then KeyAscii = 0
End Sub
'~~> Disable paste using right click
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Clipboard.Clear
End If
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
I tried to paste number it works but, when i used to type not working... How to prevent the user to paste it?
Could you make it clear..??? It is confusing us....
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India
Private Sub Text1_KeyPress(KeyAscii As Integer)
'~~> Ensures numbers are not keyed it
'~~> Also ensure the user is not able to paste number
If KeyAscii > 47 And KeyAscii < 58 Or _
KeyAscii = 22 Then KeyAscii = 0
End Sub
Pasting can also be done via Shift+Ins, which does not fire a KeyPress event.
'~~> Disable paste using right click
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Clipboard.Clear
End If
End Sub
[/CODE]
It is a bad practice to unexpectedly clear the clipboard. For example, most of us rightfully think it's cheesy and lame that opening VB6 clears the clipboard.
Also, the context menu (and by extension Paste) can be opened using the Context Menu key on the keyboard, which doesn't fire a KeyPress event.
Shift+Ins and the Context Menu keys do fire KeyDown() events, though. But even still, unexpectedly clearing the clipboard is a bad practice.
Although you should technically be allowed to paste valid text. I still maintain that clearing out invalid characters in the Change() event is the better way to handle pasting.
Does anyone know how Microsoft handles it? Are there any limited-character textboxes in Windows or Office we can play with to see how they handle pasting invalid characters?
EDIT: Microsoft allows invalid characters. For example, you can type letters into any numeric textbox under Tools=>Options in both Excel and Word, and if you right-click the desktop and go to the Screen Saver tab, you can type letters in the "Wait ... minutes" textbox.
Last edited by Ellis Dee; Feb 26th, 2010 at 10:06 AM.