473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

TextBox Validation

54
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,233547345696

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
  21.  
  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
  28.  
  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
  35.  
  36.  
Sep 23 '14 #1
2 1348
jforbes
1,107 Expert 1GB
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
  5.  
  6.     sPhone = Split(Me.Text1.Value, ",")
  7.     bTest = True
  8.  
  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
  15.  
  16.     For iCount = 0 To UBound(sPhone)
  17.         bTest = bTest And oRegEx.Test(sPhone(iCount))
  18.     Next iCount
  19.  
  20.     MsgBox "Passes Validation: " & bTest
  21.  
  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
Benniit
54
Thanks for your urgent reply and explanation.
Sep 23 '14 #3

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

Similar topics

1
by: Bryan | last post by:
I trying to figure out out to validate a textbox inside a repeater once a person presses a button on a repeater. Here's the validation Code behind: Please be aware that line e.Item.ItemIndex...
4
by: Craig G | last post by:
i have the following code for validating a textbox once a page posts, but the problem i am having is that the TextChanged event won't fire next time if the user does not change the value again ...
1
by: Mariame | last post by:
Hi Everyone I have a textbox that i created online in Form Load: "Dim TB1 as new TextBox" I want to validate that this textbox contains numbers only??? Is There is a way i can do it uing...
1
by: Kum | last post by:
Hi, I need help in asp.net dynamic textbox controls validation. I am creating textbox controls dynamically on a asp.net webpage. Now after creating the textboxes on the page I want to validate...
1
by: Eugenio.Net | last post by:
Hello all I'm using a button to validade a TextBox but I already saw some websites that validation is automaticaly after 4 or 5 chars... How can I validate my textox to call a function??? ...
0
by: slekshmipriya | last post by:
Hai friends i have a problem with my code.When my form get loaded it will contain the rollno, name and textbox to enter mark of the students.These are fetched from my table named details.The marks...
1
by: Madhumitham | last post by:
textbox validation (to enter numbers) in vb.net in both client side and server side
1
by: timnels | last post by:
I have an form that sends a message (an employee code) to another form. When that form receives the message it plops that value in a textbox and I want to run the code in the validation event of...
2
by: Durango2008 | last post by:
Hi I have written code to validate a textbox and if that textbox is valid I would like to make the submit button on the page enabled otherwise leave it disabled, which is what its default value...
9
mageswar005
by: mageswar005 | last post by:
Hi Guys, My array textbox validation is not working fine,my code is given below please help me immediately. function validation() { var chks =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.