1

I am trying to validate phone number textbox. I just want user to be able to enter only numeric values and only 10 digits in the textbox. I did it using keypress event.

it works fine but the problem is once the length of the input reaches to 10, it wont even allow the backspace.

Here is my code -

    Private Sub tbphone_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbphone.KeyPress
       If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ChrW(Keys.Back)) Or tbphone.Text.Length >= 10 Then e.Handled = True
    End Sub

Now, there can be a scenario where user enters all digits correctly but the last one. In that case he will not be able delete that last digit perhaps he will not be able to do anything in that textbox as the length of the input text is already 10 and now the e.handled is set to true.

Please suggest how can I achieve it...

Shreekant
  • 429
  • 3
  • 16
  • Instead of real-time validation, validate input value after user finished input. – Fabio Jan 14 '20 at 06:30
  • Obviously you need to change your Boolean expression to not exclude ALL keys at that length. Put some thought into what such a Boolean expression would look like. There's no sign that you've actually tried. – jmcilhinney Jan 14 '20 at 06:33
  • well thank you for your responses everyone. The problem is solved. I just set the `e.handled = false` in another `if clause`. – Shreekant Jan 14 '20 at 06:37

3 Answers3

2

Disclaimer: I have not touched vb.net in quite some time, say about a year. I am just providing a solution which I used previously and found it helpful.

You may refer to: This question has a similar issue and has been answered.

Regarding your 10 character limit, you can set the MaxLength property of the textbox either via the graphical editor or code.

You may refer to: How to set the textbox MaxLength property. (Please change the language to VB at the top of the page.)

Raymond C.
  • 572
  • 4
  • 24
  • Ohh God! How can I forget this one. Easiest way to do this. We all get stuck at silly problems sometimes. This also works for me. Thank you @Raymond – Shreekant Jan 14 '20 at 06:51
  • Not an issue, please do remember to mark the question as solved. Also if you like you may up-vote the answer in the link provided. Thanks – Raymond C. Jan 14 '20 at 07:03
1

I actually found the solution to my own question, I am putting it here so if anyone stuck validating textboxes with the same issue might get help.

I have set the e.handled to false if user presses BackSpace. And it worked...

   Private Sub tbphone_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbphone.KeyPress
       If Not Char.IsDigit(CChar(CStr(e.KeyChar))) Or tbphone.Text.Length >= 10 Then e.Handled = True
       If e.KeyChar = ChrW(Keys.Back) Then e.Handled = False
   End Sub
Shreekant
  • 429
  • 3
  • 16
0

i know it is already solved, but if i may add an answer.

    If (e.KeyChar <= ChrW(47)) Or (e.KeyChar >= ChrW(58)) Then
        If e.KeyChar = vbBack Then
        Else
            e.KeyChar = ChrW(0)
        End If
    Else
    End If

i use this code in keypress event

Eric Sugiharto
  • 21
  • 1
  • 12
  • I already have this in my code you can check it. My problem was with the length. if we limit it to 10 digits it was not allowing me to press even back space. But anyhow it is solved now. Thank You For your reply – Shreekant Jan 14 '20 at 10:39