i need to validate the textbox such that in can accept numbers only but the entered no should be greater than 0 , it can be 0.1 and should be less than 101.

Recommended Answers

All 2 Replies

Subscribe to KeyPress event hander of textBox control and use this code inside of it:

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

And to set max of age to 100 you can do it like:

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)
		If Not e.Handled Then
			Dim age As Integer
			If Integer.TryParse(textBox1.Text + e.KeyChar.ToString(), age) Then
				e.Handled = If(age < 101, False, True)
			End If
		End If
	End If
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.