Skip to main content
2 of 5
deleted 4 characters in body; edited tags
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Is this a better way to validate a text box for mobile number only?

Is this a better way to validate a text box for mobile number only?

Validation:

  • allow only numbers
  • will not allow to paste string including characters
  • allow copy from the box
  • length is set to 10 without country code
  • allow back space

Suggest some methods to help me improve my function.

Public Sub mob_validation(ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal mob As TextBox)
If e.KeyChar = Chr(22) Then
Dim output As String = New String((From c As Char In Clipboard.GetText.ToString Select c Where Char.IsLetter(c)).ToArray())
If output = "" And Len(mob.Text) = 10 Then
mob.ForeColor = Color.Green
Else
mob.ForeColor = Color.Red
Clipboard.Clear()
End If
Else
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) <> 3 Then
e.Handled = True
mob.ForeColor = Color.Red
End If
End If
Else
mob.ForeColor = Color.Green
End If
End If
End Sub