Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Help with code for telephone validator

Help with code for telephone validator

Scheduled Pinned Locked Moved Visual Basic
help
28 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 1 Offline
    1 Offline
    12String
    wrote on last edited by
    #1

    Can someone out there give a fellow a helping hand. I am desparately trying to code for a telephone validator. My form1 has only two objects, a textbox and a button. Validate a phonenumber 999-999-9999 Where I am running into a snag is how and where do I create the substrings for the areacode, prefix, and phonenumber plus incorporating the "-" in char positions (3,1) and (7,1) Can someone give me some assistance or better yet help me code the whole tamole. LOL. 12String 12String

    B 1 C S 4 Replies Last reply
    0
    • 1 12String

      Can someone out there give a fellow a helping hand. I am desparately trying to code for a telephone validator. My form1 has only two objects, a textbox and a button. Validate a phonenumber 999-999-9999 Where I am running into a snag is how and where do I create the substrings for the areacode, prefix, and phonenumber plus incorporating the "-" in char positions (3,1) and (7,1) Can someone give me some assistance or better yet help me code the whole tamole. LOL. 12String 12String

      B Offline
      B Offline
      Brett Peirce
      wrote on last edited by
      #2

      what do you mean by validate? making sure that it is a number that will reach someone? or just making sure it is 3 digits followed by a dash followed by 3 more digits etc...? or perhaps something else?


      sincerely, Brett Peirce - PolerBear To err is human; To forgive: divine.

      1 1 Reply Last reply
      0
      • 1 12String

        Can someone out there give a fellow a helping hand. I am desparately trying to code for a telephone validator. My form1 has only two objects, a textbox and a button. Validate a phonenumber 999-999-9999 Where I am running into a snag is how and where do I create the substrings for the areacode, prefix, and phonenumber plus incorporating the "-" in char positions (3,1) and (7,1) Can someone give me some assistance or better yet help me code the whole tamole. LOL. 12String 12String

        1 Offline
        1 Offline
        12String
        wrote on last edited by
        #3

        What I need to do is verify the first three 999 the first "-" second 999 second "-" and last four 9999. In the frmMain I have a textbox and a button. when the user enters a phone number 999-999-9999 the program will display one of the following messages: "You must enter a numeric telephone number starting with the area code." "You must enter a "-" after the area code." "YOu must enter a "-" after the prefix." "Invalid area code, please try again." "Invalid prefix, please try again." "Phone number is valid." My problem is I have set the char to 12 in the properties for textbox1 and I need to create substrings for area code, prefix, phonenumber, and incorporate the two dashes "-" Can you help me!

        1 Reply Last reply
        0
        • B Brett Peirce

          what do you mean by validate? making sure that it is a number that will reach someone? or just making sure it is 3 digits followed by a dash followed by 3 more digits etc...? or perhaps something else?


          sincerely, Brett Peirce - PolerBear To err is human; To forgive: divine.

          1 Offline
          1 Offline
          12String
          wrote on last edited by
          #4

          What I need to do is verify the first three 999 the first "-" second 999 second "-" and last four 9999. In the frmMain I have a textbox and a button. when the user enters a phone number 999-999-9999 the program will display one of the following messages: "You must enter a numeric telephone number starting with the area code." "You must enter a "-" after the area code." "YOu must enter a "-" after the prefix." "Invalid area code, please try again." "Invalid prefix, please try again." "Phone number is valid." My problem is I have set the char to 12 in the properties for textbox1 and I need to create substrings for area code, prefix, phonenumber, and incorporate the two dashes "-" Can you help me!

          D 1 Reply Last reply
          0
          • 1 12String

            Can someone out there give a fellow a helping hand. I am desparately trying to code for a telephone validator. My form1 has only two objects, a textbox and a button. Validate a phonenumber 999-999-9999 Where I am running into a snag is how and where do I create the substrings for the areacode, prefix, and phonenumber plus incorporating the "-" in char positions (3,1) and (7,1) Can someone give me some assistance or better yet help me code the whole tamole. LOL. 12String 12String

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            The best way to validate a phone number is to use a regex. You don't need to mangle the string at all, but if you wanted to, string.split would be the easiest way here. Christian Graus - Microsoft MVP - C++

            1 1 Reply Last reply
            0
            • C Christian Graus

              The best way to validate a phone number is to use a regex. You don't need to mangle the string at all, but if you wanted to, string.split would be the easiest way here. Christian Graus - Microsoft MVP - C++

              1 Offline
              1 Offline
              12String
              wrote on last edited by
              #6

              Believe me I wish I could but the prof wants us to use VB.NET and write the program and that is why I am stuck, I cannot figure it out. Can you assist me?

              C 1 Reply Last reply
              0
              • 1 12String

                Believe me I wish I could but the prof wants us to use VB.NET and write the program and that is why I am stuck, I cannot figure it out. Can you assist me?

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Regular expressions are built into VB.NET. Are you not allowed to use those ? The other way to do it would be to use the Split method on the string that has the phone number, and use the double.TryParse method to see if they are all numbers. You can specify to not allow decimal points. The only other step is to verify the length of hte strings, and how many groups there are. A regex will do this all for you in one go. Something like \d{3}-\d{3}-\d{4} should do it, and you can change it so the - can be a space, or whatever. Christian Graus - Microsoft MVP - C++

                1 1 Reply Last reply
                0
                • C Christian Graus

                  Regular expressions are built into VB.NET. Are you not allowed to use those ? The other way to do it would be to use the Split method on the string that has the phone number, and use the double.TryParse method to see if they are all numbers. You can specify to not allow decimal points. The only other step is to verify the length of hte strings, and how many groups there are. A regex will do this all for you in one go. Something like \d{3}-\d{3}-\d{4} should do it, and you can change it so the - can be a space, or whatever. Christian Graus - Microsoft MVP - C++

                  1 Offline
                  1 Offline
                  12String
                  wrote on last edited by
                  #8

                  I tried that and it did not like it. LOL I guess VB.NET is a little more sensitive LOL. Did you look at my code? How can I manipulate the IsNumeric function to incorporate the dashes using the IndexOf method. See my problem is I have the keys to it but not the combination. I am just a greenie. Sorry. 12String

                  C 1 Reply Last reply
                  0
                  • 1 12String

                    I tried that and it did not like it. LOL I guess VB.NET is a little more sensitive LOL. Did you look at my code? How can I manipulate the IsNumeric function to incorporate the dashes using the IndexOf method. See my problem is I have the keys to it but not the combination. I am just a greenie. Sorry. 12String

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    12String wrote:

                    How can I manipulate the IsNumeric function to incorporate the dashes using the IndexOf method.

                    That's a real hack, I would not use it. The answer though would be to use split to split into a number of substrings, by the -, and then stepping through each string.

                    12String wrote:

                    I tried that and it did not like it.

                    A regex ? Then you need to work with it until it does work. That was off the top of my head. However, I just tested it, and it works fine, assuming the number is xxx-xxx-xxxx where x is a number. Did I get that wrong ? What was your code ? Christian Graus - Microsoft MVP - C++

                    1 1 Reply Last reply
                    0
                    • C Christian Graus

                      12String wrote:

                      How can I manipulate the IsNumeric function to incorporate the dashes using the IndexOf method.

                      That's a real hack, I would not use it. The answer though would be to use split to split into a number of substrings, by the -, and then stepping through each string.

                      12String wrote:

                      I tried that and it did not like it.

                      A regex ? Then you need to work with it until it does work. That was off the top of my head. However, I just tested it, and it works fine, assuming the number is xxx-xxx-xxxx where x is a number. Did I get that wrong ? What was your code ? Christian Graus - Microsoft MVP - C++

                      1 Offline
                      1 Offline
                      12String
                      wrote on last edited by
                      #10

                      Here is what I have come up with so far and this doesn't quite work. What do you think. I still need to code in VB.NET so therein lays my problem. LOL Public Class frmMain Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents btnValPhNmb As System.Windows.Forms.Button Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.btnValPhNmb = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(16, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(264, 24) Me.Label1.TabIndex = 0 Me.Label1.Text = "Enter Phone Number" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'TextBox1 ' Me.TextBox1.Font = New System.Drawing.Font("Times New Roman", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.TextBox1.Location = New System.Drawing.Point(80, 56) Me.TextBox1.MaxLength = 12 Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(128, 26) Me.TextBox1.TabIndex = 1 Me.TextBox1.

                      C 1 Reply Last reply
                      0
                      • 1 12String

                        Here is what I have come up with so far and this doesn't quite work. What do you think. I still need to code in VB.NET so therein lays my problem. LOL Public Class frmMain Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents btnValPhNmb As System.Windows.Forms.Button Private Sub InitializeComponent() Me.Label1 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.btnValPhNmb = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(16, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(264, 24) Me.Label1.TabIndex = 0 Me.Label1.Text = "Enter Phone Number" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'TextBox1 ' Me.TextBox1.Font = New System.Drawing.Font("Times New Roman", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.TextBox1.Location = New System.Drawing.Point(80, 56) Me.TextBox1.MaxLength = 12 Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(128, 26) Me.TextBox1.TabIndex = 1 Me.TextBox1.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        12String wrote:

                        I still need to code in VB.NET so therein lays my problem

                        Yeah, VB sucks, but it can sure do this as easily as any other language ( and easier than vanilla C++ )

                        12String wrote:

                        If (Not IsNumeric(Me.TextBox1.Text)) Then 'checks user input for numeric and range

                        This plainly does not work. You should try to follow my advice, and either use a regex, or, if you must, split the string and validate it from there.

                        12String wrote:

                        If TextBox1 = ("999" & "-" & "999" & "-" & "9999") Then MsgBox("You have entered a correct phone number.") ElseIf TextBox1 = "999999" & "-" & 9999 Then MsgBox("You need to enter a " - " between the area code and prefix number.") ElseIf TextBox1 = "999" & "-" & "9999999" Then MsgBox("You need to enter a " - " between the prefix and phonenumber.") End If

                        This requires the numbers to be all nines. Your check input function should check all cases, you shouldn't be adding validation afterwards. The regex can be used to do this as well, although the split solution would make it easier to do this and give a detailed error. Christian Graus - Microsoft MVP - C++

                        1 1 Reply Last reply
                        0
                        • C Christian Graus

                          12String wrote:

                          I still need to code in VB.NET so therein lays my problem

                          Yeah, VB sucks, but it can sure do this as easily as any other language ( and easier than vanilla C++ )

                          12String wrote:

                          If (Not IsNumeric(Me.TextBox1.Text)) Then 'checks user input for numeric and range

                          This plainly does not work. You should try to follow my advice, and either use a regex, or, if you must, split the string and validate it from there.

                          12String wrote:

                          If TextBox1 = ("999" & "-" & "999" & "-" & "9999") Then MsgBox("You have entered a correct phone number.") ElseIf TextBox1 = "999999" & "-" & 9999 Then MsgBox("You need to enter a " - " between the area code and prefix number.") ElseIf TextBox1 = "999" & "-" & "9999999" Then MsgBox("You need to enter a " - " between the prefix and phonenumber.") End If

                          This requires the numbers to be all nines. Your check input function should check all cases, you shouldn't be adding validation afterwards. The regex can be used to do this as well, although the split solution would make it easier to do this and give a detailed error. Christian Graus - Microsoft MVP - C++

                          1 Offline
                          1 Offline
                          12String
                          wrote on last edited by
                          #12

                          How do you recommend I code that? The first line then I gather is correct, yes? If TextBox1 = ("999" & "-" & "999" & "-" & "9999") Then 12String

                          C 1 Reply Last reply
                          0
                          • 1 12String

                            How do you recommend I code that? The first line then I gather is correct, yes? If TextBox1 = ("999" & "-" & "999" & "-" & "9999") Then 12String

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #13

                            No, that is useless. It will only accept one string. Why bother concatenating 5 values to check if the user typed 999-999-9999 ? How often would that be the number the type ? I'm not sure in VB, but in C# it's string [] strings = theString.Split("-"); I suspect it would be string () strings = theString.Split("-") but I could be wrong. Then you can check if the array has three elements, and what their lengths are. A regex will tell you if they followed the format, but not how they failed to follow it. It depends on how much detail you want in your error message. Christian Graus - Microsoft MVP - C++ -- modified at 23:11 Thursday 27th October, 2005

                            1 1 Reply Last reply
                            0
                            • C Christian Graus

                              No, that is useless. It will only accept one string. Why bother concatenating 5 values to check if the user typed 999-999-9999 ? How often would that be the number the type ? I'm not sure in VB, but in C# it's string [] strings = theString.Split("-"); I suspect it would be string () strings = theString.Split("-") but I could be wrong. Then you can check if the array has three elements, and what their lengths are. A regex will tell you if they followed the format, but not how they failed to follow it. It depends on how much detail you want in your error message. Christian Graus - Microsoft MVP - C++ -- modified at 23:11 Thursday 27th October, 2005

                              1 Offline
                              1 Offline
                              12String
                              wrote on last edited by
                              #14

                              All of the error messages will be handled via MsgBox message, that is why I have to code for the dashes and the numbers. Now should I code this all under the button1.clicked function or should I delcare my TextBox as a global variable then I can code everything under the button1.clicked Private Sub?

                              C 1 Reply Last reply
                              0
                              • 1 12String

                                All of the error messages will be handled via MsgBox message, that is why I have to code for the dashes and the numbers. Now should I code this all under the button1.clicked function or should I delcare my TextBox as a global variable then I can code everything under the button1.clicked Private Sub?

                                C Offline
                                C Offline
                                Christian Graus
                                wrote on last edited by
                                #15

                                12String wrote:

                                All of the error messages will be handled via MsgBox message, that is why I have to code for the dashes and the numbers.

                                Yes, but you don't need to code it the way you have, and you shouldn't code it outside the function that validates the input.

                                12String wrote:

                                Now should I code this all under the button1.clicked function

                                As another function, I think so.

                                12String wrote:

                                should I delcare my TextBox as a global variable

                                This is always a hideous idea. You have what you need in the checknumeric function, except what's in it is wrong. Remove the other code with the messageboxes, and instead add something which will work inside that function. Christian Graus - Microsoft MVP - C++

                                1 1 Reply Last reply
                                0
                                • C Christian Graus

                                  12String wrote:

                                  All of the error messages will be handled via MsgBox message, that is why I have to code for the dashes and the numbers.

                                  Yes, but you don't need to code it the way you have, and you shouldn't code it outside the function that validates the input.

                                  12String wrote:

                                  Now should I code this all under the button1.clicked function

                                  As another function, I think so.

                                  12String wrote:

                                  should I delcare my TextBox as a global variable

                                  This is always a hideous idea. You have what you need in the checknumeric function, except what's in it is wrong. Remove the other code with the messageboxes, and instead add something which will work inside that function. Christian Graus - Microsoft MVP - C++

                                  1 Offline
                                  1 Offline
                                  12String
                                  wrote on last edited by
                                  #16

                                  So I should leave the Public Sub CheckInput() as is and delete the above msgBoxes? any suggestions on what to put in thier place LOL.

                                  C 1 Reply Last reply
                                  0
                                  • 1 12String

                                    So I should leave the Public Sub CheckInput() as is and delete the above msgBoxes? any suggestions on what to put in thier place LOL.

                                    C Offline
                                    C Offline
                                    Christian Graus
                                    wrote on last edited by
                                    #17

                                    12String wrote:

                                    any suggestions on what to put in thier place LOL.

                                    Exactly what I've suggested already - you can split the strings by the -, and then check the rest is numbers of the right length, or use a regex. Christian Graus - Microsoft MVP - C++

                                    1 1 Reply Last reply
                                    0
                                    • C Christian Graus

                                      12String wrote:

                                      any suggestions on what to put in thier place LOL.

                                      Exactly what I've suggested already - you can split the strings by the -, and then check the rest is numbers of the right length, or use a regex. Christian Graus - Microsoft MVP - C++

                                      1 Offline
                                      1 Offline
                                      12String
                                      wrote on last edited by
                                      #18

                                      The problem is the how. I do not know how to code it. Exactly how would you code it. I cannot find any examples or close to examples in this text I have it is most frustrating.

                                      C 1 Reply Last reply
                                      0
                                      • 1 12String

                                        The problem is the how. I do not know how to code it. Exactly how would you code it. I cannot find any examples or close to examples in this text I have it is most frustrating.

                                        C Offline
                                        C Offline
                                        Christian Graus
                                        wrote on last edited by
                                        #19

                                        The regex I provided will work with Regex.IsMatch ( from memory ), otherwise I've given you the syntax for the split function, and said that you need to use Double.TryParse to see if they are numnbers. I'm reluctant to do much more, that should be enough for you to find out the rest, and as this is homework, the point is learning, not just doing. Christian Graus - Microsoft MVP - C++

                                        1 1 Reply Last reply
                                        0
                                        • C Christian Graus

                                          The regex I provided will work with Regex.IsMatch ( from memory ), otherwise I've given you the syntax for the split function, and said that you need to use Double.TryParse to see if they are numnbers. I'm reluctant to do much more, that should be enough for you to find out the rest, and as this is homework, the point is learning, not just doing. Christian Graus - Microsoft MVP - C++

                                          1 Offline
                                          1 Offline
                                          12String
                                          wrote on last edited by
                                          #20

                                          Herein lies another problem, when I enter the string()strings=string.split("-") VB gives me a syntax error stating that 'String is a class type, and so is not valid. Also Name 'TheString'is not declared. UFFFFF! You see my frustration. LOL

                                          C 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups