Blog : To validate textbox allows only numeric and decimal in vb.net

to validate textbox allows only numeric and decimal in vb.net

i need to validate a textbox
it should take either decimal values or integers and max length is 16
if it takes decimal value as input then it should contain only 14 digits in decimal place and 2 digits in fraction part
-----------------------------------------------------------------------------
how to validate textbox allows only numeric and decimal in vb.net
What you're asking for is not as simple as you think. Here's some code that will get you most of the way there, but it still won't stop the user pasting invalid data in:
vb.net Code:
1.Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
2.  Dim keyChar = e.KeyChar3. 4.  If Char.IsControl(keyChar) Then5.  'Allow all control characters.6.  ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then7.  Dim text = Me.TextBox1.Text8.  Dim selectionStart = Me.TextBox1.SelectionStart9.  Dim selectionLength = Me.TextBox1.SelectionLength10. 11.  text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)12. 13.  If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then14.  'Reject an integer that is longer than 16 digits.15.  e.Handled = True16.  ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then17.  'Reject a real number with two many decimal places.18.  e.Handled = True19.  End If20.  Else21.  'Reject all other characters.22.  e.Handled = True23.  End If24.End Sub
Private Sub txtname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
If AscW(e.KeyChar) > 64 And AscW(e.KeyChar) < 91 Or AscW(e.KeyChar) > 96 And AscW(e.KeyChar) < 123 Or AscW(e.KeyChar) = 8 Then
  Else
  e.KeyChar = Nothing
  End If
End Sub