By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

TextBox Validation

P: 54
Please I'm using 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:

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

Expand|Select|Wrap|Line Numbers
  1.   Dim str1 As String = Mid(Me.txtRecipients.Text, 1, 1) 
  2.         Dim str2 As String = Mid(Me.txtRecipients.Text, 2, 1)
  3.         Dim str3 As String = Mid(Me.txtRecipients.Text, 3, 1)
  4.         Dim str4 As String = Mid(Me.txtRecipients.Text, 4, 1)
  5.         Dim str13 As String = Mid(Me.txtRecipients.Text, 13, 1)
  6.         If str1.Trim <> 2 Then
  7.             Me.txtRecipients.Focus()
  8.             MsgBox(The first digit must be 2)
  9.             Exit Sub
  10.         End If
  11.         If str2.Trim <> 3 Then
  12.             Me.txtRecipients.Focus()
  13.             MsgBox(The second digit must be 3)
  14.             Exit Sub
  15.         End If
  16.         If str3.Trim <> 3 Then
  17.             Me.txtRecipients.Focus()
  18.             MsgBox(The third digit must be 3)
  19.             Exit Sub
  20.         End If
  22.         If str4 = 0 Then
  23.             Me.txtRecipients.Text.Replace(0, String.Empty)
  24.             Me.txtRecipients.Focus()
  25.             MsgBox(Phone No. must not start with 0. Format e.g. 233243404804)
  26.             Exit Sub
  27.         End If
  29.         If str13 <> "," Then
  30.             ' Me.txtRecipients.Text.Replace(str13, ",")
  31.             Me.txtRecipients.Focus()
  32.             MsgBox(Phone No. must not start with 0. Format e.g. 233243404804)
  33.             Exit Sub
  34.         End If
Sep 23 '14 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 1,107
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.     Dim sPhone() As String
  3.     Dim iCount As Integer
  4.     Dim bTest As Boolean
  6.     sPhone = Split(Me.Text1.Value, ",")
  7.     bTest = True
  9.     'RegEx Dim Start
  10.     Dim oRegEx As Object
  11.     Set RegEx = CreateObject("VBScript.RegExp")
  12.     oRegEx.Global = True
  13.     oRegEx.Pattern = "^233[1-9](\d{8})$"
  14.     'RegEx Dim End
  16.     For iCount = 0 To UBound(sPhone)
  17.         bTest = bTest And oRegEx.Test(sPhone(iCount))
  18.     Next iCount
  20.     MsgBox "Passes Validation: " & bTest
  22. 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:

Expand|Select|Wrap|Line Numbers
  1. Dim oRegEx As Regex = New Regex("^233[1-9](\d{8})$")
and put the Import at the Top of you Module:
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 23 '14 #2

P: 54
Thanks for your urgent reply and explanation.
Sep 23 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.