0

I have created a textbox that inputs contact number and i set it to maxlength = 11. how do i prevent users from entering less than 11 digits? just only exact 11 digits. Thanksssss

2
  • google is your friend, I looked for " vb.net textbox min length ". Take a peek at stackoverflow.com/questions/44497302/… Commented Nov 17, 2019 at 6:12
  • thanks! It's very useful to understand my problem right now. :D Commented Nov 17, 2019 at 6:22

3 Answers 3

1

You can consider using a MaskedTextBox - it will allow you to specify that 11 digits must be entered, and it can even have separator chars like hyphens for phone numbers. I linked to MSDN - take a look

As a side note, consider NOT using a MessageBox to give the user feedback if they enter a wrong value /incomplete phone number - message boxes are quite annoying and interrupt the flow of a user interface. The modern way is to show some highlighted warning next to the input field rather than showing something that stops user input and must be dismissed, because this interrupts the user's train of thought

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the additional info! I will not use MessageBox for validations in the future. :D
With regards to not using message boxes, you might consider using the ErrorProvider component.
This is really something I was in need of badly! Thank You @Caius Jard
0

Generally speaking, you should perform validation in the Validating event handler. If the data fails validation then you cancel the event and the control will not lose focus. You can call ValidateChildren before using the data, to ensure that even controls that never received focus are validated.

Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs) Handles TextBox1.Validating If TextBox1.TextLength < TextBox1.MaxLength OrElse TextBox1.Text.Any(Function(ch) Not Char.IsDigit(ch)) Then TextBox1.HideSelection = False TextBox1.SelectAll() MessageBox.Show($"Please enter an {TextBox1.MaxLength}-digit number", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error) TextBox1.HideSelection = True e.Cancel = True End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If ValidateChildren() Then 'All data is valid so it is safe to use it. End If End Sub 

Comments

0

I've figure it out the answer:

I've set my Txtbox.Contact to maxlength = 11 in the Properties Design option.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Txtbox_Contact.TextLength = 11 Then MessageBox.Show("Contact Number is Accepted") Else MessageBox.Show("Please input 11 digit number!") End If End Sub 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.