Hi i want to prevent the user from entering number and special charaters in a textbox and another textbox from entering alphabets and specials characters .....
Need it quickly coz 2moro have to submit my project...
Hi i want to prevent the user from entering number and special charaters in a textbox and another textbox from entering alphabets and specials characters .....
Need it quickly coz 2moro have to submit my project...
Nice starting points from , , and . One caution: KeyPress filtering only catches keystrokes. Users can still paste, drag-drop, or your code can set the Text property directly. Also, the thread is tagged asp.net:vb.net, while those handlers are WinForms. Here are safer, minimal patterns for both cases.
ASP.NET Web Forms (validate on the page and again on the server):
<asp:TextBox ID="txtNumbers" runat="server" />
<asp:RegularExpressionValidator runat="server"
ControlToValidate="txtNumbers"
ValidationExpression="^[0-9]+$"
ErrorMessage="Numbers only." />
<asp:TextBox ID="txtLetters" runat="server" />
<asp:RegularExpressionValidator runat="server"
ControlToValidate="txtLetters"
ValidationExpression="^[A-Za-z ]+$"
ErrorMessage="Letters and spaces only." /> ' Always re-check server-side
If Not System.Text.RegularExpressions.Regex.IsMatch(txtNumbers.Text, "^[0-9]+$") Then
' reject or show message
End If WinForms (validate the full value, not individual keys):
Imports System.ComponentModel
Imports System.Text.RegularExpressions
Private ReadOnly reDigits As New Regex("^[0-9]+$")
Private ReadOnly reLetters As New Regex("^[A-Za-z ]+$")
Private Sub TextBoxNumbers_Validating(sender As Object, e As CancelEventArgs) _
Handles TextBoxNumbers.Validating
e.Cancel = Not reDigits.IsMatch(TextBoxNumbers.Text)
End Sub
Private Sub TextBoxLetters_Validating(sender As Object, e As CancelEventArgs) _
Handles TextBoxLetters.Validating
e.Cancel = Not reLetters.IsMatch(TextBoxLetters.Text)
End Sub Tips:
[0-9] if you want ASCII digits only; \d can match other Unicode digits.ShortcutsEnabled = False, but still keep the Validating check.MaskedTextBox can be simpler than custom code.Jump to Post— Jx_Man 987This following code just allowed you to entered numbers only (No alphabetics or any special characters) :
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _ Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then e.Handled = True End If If (Microsoft.VisualBasic.Asc(e.KeyChar) = …
This following code just allowed you to entered numbers only (No alphabetics or any special characters) :
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub This following code just allowed you to entered strings / alphabetics only (no numbers or any special characters):
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
'Allowed space
If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
e.Handled = True
End If
End If
' Allowed backspace
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub To make text box to accept only numbers, in key press event of that textbox u can code like this
If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
e.Handled = True
MsgBox("Please enter valid number ")
End If
i think u can mannage other one..
Hi i want to prevent the user from entering number and special charaters in a textbox and another textbox from entering alphabets and specials characters .....
Need it quickly coz 2moro have to submit my project...
--------
Use the following code..
#Region "TextBox Events"
Private Sub txtFinRefNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFinRefNo.KeyPress
Try
If Char.IsLetterOrDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
e.Handled = True
End If
Catch ex As Exception
ShowException(ex.Message, MESSAGEBOX_TITLE, ex)
End Try
End Sub
#End Region
sabeer pasha.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.