TextBox Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benniit
    New Member
    • May 2008
    • 54

    TextBox Validation

    Please I'm using vb.net 2013 and I have a textbox named txtRecipients

    Now i want to check for the following:

    1. The phone no. should always start with 233 followed by any digit other than zero like: 233201245685
    2. Multiple phone numbers should be separated by a comma, like 233201245685, and the comma replaced automatically before the start of another phone number in the same format. So if two phone numbers are entered they should be like this:
    233201245685,23 3547345696

    I did something like this in the leave event. Is there any better way to do this as the comma is not getting replaced at a specific position. I used both the remove and replaced functions, but that did not work. Thanks in advance

    Code:
      Dim str1 As String = Mid(Me.txtRecipients.Text, 1, 1) 
            Dim str2 As String = Mid(Me.txtRecipients.Text, 2, 1)
            Dim str3 As String = Mid(Me.txtRecipients.Text, 3, 1)
            Dim str4 As String = Mid(Me.txtRecipients.Text, 4, 1)
            Dim str13 As String = Mid(Me.txtRecipients.Text, 13, 1)
            If str1.Trim <> 2 Then
                Me.txtRecipients.Focus()
                MsgBox(The first digit must be 2)
                Exit Sub
            End If
            If str2.Trim <> 3 Then
                Me.txtRecipients.Focus()
                MsgBox(The second digit must be 3)
                Exit Sub
            End If
            If str3.Trim <> 3 Then
                Me.txtRecipients.Focus()
                MsgBox(The third digit must be 3)
                Exit Sub
            End If
    
            If str4 = 0 Then
                Me.txtRecipients.Text.Replace(0, String.Empty)
                Me.txtRecipients.Focus()
                MsgBox(Phone No. must not start with 0. Format e.g. 233243404804)
                Exit Sub
            End If
    
            If str13 <> "," Then
                ' Me.txtRecipients.Text.Replace(str13, ",")
                Me.txtRecipients.Focus()
                MsgBox(Phone No. must not start with 0. Format e.g. 233243404804)
                Exit Sub
            End If
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    I currently don't have Visual Studio installed on my computer. (I know, it's a crying shame) But, I put together this VBA example of a Regular Expression that should work for your case.

    Regular Expressions can be very confusing, but in a situation like this it takes care of a bunch of the IF Statements for you.

    This is a piece of code that I threw into MS-Access VBA that validates the phone number to your Pattern. It does it all at once, so you loose the granularity of exactly where the validation fails, but I personally wouldn't spend the time of pointing out exactly what is wrong. I would just post the Format expected then Validate against it.

    Code:
    Private Sub Command0_Click()
        Dim sPhone() As String
        Dim iCount As Integer
        Dim bTest As Boolean
        
        sPhone = Split(Me.Text1.Value, ",")
        bTest = True
    
        'RegEx Dim Start
        Dim oRegEx As Object
        Set RegEx = CreateObject("VBScript.RegExp")
        oRegEx.Global = True
        oRegEx.Pattern = "^233[1-9](\d{8})$"
        'RegEx Dim End
        
        For iCount = 0 To UBound(sPhone)
            bTest = bTest And oRegEx.Test(sPhone(iCount))
        Next iCount
        
        MsgBox "Passes Validation: " & bTest
        
    End Sub
    In Visual Basic, you'll probably want to use Early Binding so instead of all the RegEx Dim Lines you can define your oRegEx variable something like:

    Code:
    Dim oRegEx As Regex = New Regex("^233[1-9](\d{8})$")
    and put the Import at the Top of you Module:
    Code:
    Imports System.Text.RegularExpressions
    It's been a while since I've done this, so I hope I've led you down the right path.

    Comment

    • Benniit
      New Member
      • May 2008
      • 54

      #3
      Thanks for your urgent reply and explanation.

      Comment

      Working...