# 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

• Please indent your code as I already told you in another review. It's even unreadable for a review! – ComFreek Aug 3 '14 at 15:54
• use regex that will better – paritosh Aug 3 '14 at 16:04
• @paritosh - your suggested edit would make a better answer... code indentation is a reviewable aspect of the question – rolfl Aug 3 '14 at 16:20
• With all good intentions: I've been seeing this user over at SO all day posting/answering a lot of questions. And believe you me, if you ever see any hints of indenting, it's by accident. IMO, for the good of the community (SE), he should be taught to indent the code. – Bjørn-Roger Kringsjå Aug 3 '14 at 16:49
• Great! Downvote revoked. Keep in mind that good formatted questions/answers tends to gain more votes;) – Bjørn-Roger Kringsjå Aug 3 '14 at 16:59

Other than that, the biggest issue with this is all of the magic numbers. They mean absolutely nothing to Mr. Maintainer. Normally at this point, I tell people to create an Enum, but in this case, there's one built in. Use the Keys Enumeration to replace all of those hardcoded numbers.