Hi everyone! How do I make a textBox control to validate a tel nr field? For instance When the user enters 0127748222, It should be displayed as (012) 774-8222 in the textbox.
I need this to be done this way since this is the way i'll be storing the fields in the database.

Thanks

Recommended Answers

All 3 Replies

Hi, Object.ToString() to format the string

I won't guarantee that this code is bullet-proof, but give it a try:

Dim TempStr As String
Dim i As Integer
Const DIGITS As String = "0123456789"

TempStr = TextBox1.Text
' Strip to only digits. There may be a better way to remove non-digits off
Do Until String.IsNullOrEmpty(TempStr)
  For i = 0 To TempStr.Length - 1
    If DIGITS.IndexOf(TempStr(i)) < 0 Then
      ' Not digit, remove it
      TempStr = TempStr.Replace(TempStr(i), "")
      Continue Do
    End If
  Next i
  Exit Do
Loop
' Use custom format string
If Not String.IsNullOrEmpty(TempStr) Then
  TextBox1.Text = CLng(TempStr).ToString("'('0##')' ###'-'####")
End If

Thanx y'all, I Will do try your solutions, they seem to be right. And oh me, little did I realise that there was a masked textbox. Eish!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.