Hey Everyone,

I have a text box, and i want to make sure the user doesnt ruin my program by putting in random chars instead of just numeric values.

Now i believe using something like

Private Sub Text1_KeyPress(ByVal KeyAscii As Integer)
        If KeyAscii <> vbKeyBack Then
            If Not IsNumeric(Chr(KeyAscii)) Then
                KeyAscii = 0
            End If
        End If
    End Sub

However i am using Visual Web Developer and it does not recognise _Keypress!!!1, i have the option to use AJAX but it will mess up the rest of my coding because everything else pretty much works and will work out very complicated to change it all

Can anyone help please?

thank you :D :-/

6 Years
Discussion Span
Last Post by crishjeny

generally in this case no code to be written in asp.net as you can use expression validator to check for your inputs at client side. try dis:


Hope this helps


Check out this code:

Private Sub textBox1_KeyPress(sender As Object, e As KeyPressEventArgs)
	'allows backspace key
	If e.KeyChar <> ControlChars.Back Then
		'allows just number keys
		e.Handled = Not Char.IsNumber(e.KeyChar)
	End If
End Sub

<asp:RegularExpressionValidator ID="REtxtMobileNo" ControlToValidate="txtMobileNo"
ValidationExpression="^((-)?[0-9]+(\.[0-9]+)?)$" Display="None" runat="server"
ErrorMessage="Invalid Mobile Phone"></asp:RegularExpressionValidator>

try this code

Edited by Narue: Snipped fake signature

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.