user1795999
user1795999

Reputation: 301

validate textbox to accept numbers only upto 8 places

I have a text box where phone number must be entered I want to limit the digits to be entered in the text box only upto 8 places how to do it?

Upvotes: 1

Views: 3361

Answers (3)

Master Chief
Master Chief

Reputation: 2541

Use a MaskedTextBox. See this link. Though the example is for C# it will work similarly. Your Mask property will be 00000000

for e.g.

myMaskTextBox.Mask = "00000000";

You can use it format data for dates, currencies etc. Also there is a handy BeepOnError property. Again see at the end of example.

Upvotes: 0

Parimal Raj
Parimal Raj

Reputation: 20585

You can subscribe for the KeyDown event to see which key is pressed, then just allow Numeric & backspace key

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.MaxLength = 8
    AddHandler TextBox1.KeyDown, AddressOf HandleTbKeyDown


End Sub

Private Sub HandleTbKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    If Not ((e.KeyValue >= 48 AndAlso e.KeyValue <= 57) OrElse e.KeyValue = 46) Then
        e.Handled = True
    End If
End Sub

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172568

For text box having only numbers you can do by this:-

if(!((e.keyCode>=48&&e.keyCode<=57)||(e.keyCode==46)))

Also you can check the length to 8 like

Texbox MaxLength = 8

Upvotes: 0

Related Questions