borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Adding Papers to Printers.)Next Topic (about pointing record in a datagrid) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Coding using IsNumeric Solved Topic
Poster Message
12String
Level: Protégé


Registered: 28-10-2005
Posts: 4

icon Coding using IsNumeric

I am currently working on a project that is due by Saturday, YIKES!!! Having the dickens of a time getting it coded. Here are the requirements:
Develop a program that validates the value a user enters into a TextBox control to ensure that the entry is a valid telephone number. When the user clicks a button, the program should determine if the entry is of the form 999-999-9999, where the character 9 represents any number.

If the entry is determinded to be a telephone number, display an appropriate message, along with the deparate substrings that compose the telephone number without the dashes. If the entry is not a telephone number, then display a message as to the reason why the entry is not a valid telephone number, clear the TextBox control and set focus to the TextBox control. Use String class methods to solve the problem.

Following these guidelines:

String variables
Substring and Length() methods.
MsgBox()
Focus():
             syntax: control.Focus(), where control is the name of
             a control that will receive focus during runtime.
             Example:  cmbCreditRating.Focus()

The cmbCreditRating control receives focus when the above is executed

             IsNumeric() does not filter dash"-" characters so your program must use IndexOf to filter for these characters in your substrings

Logic:

First check the length of the input string: it must be exactly 12
Then check for the Substring(3,1) and Substring(7,1), i.e. the foruth and eighth characters must be "-"

Use the Substring method to separate the input string into 3 parts
Check to see if each part is only numeric characters, using the IsNumeric and IndexOf.
If error display messagebox

Must use if-then-else statment

Here is the code I have so far:
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
    <System.Diagnostics.DebuggerStepThrough()> 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.Text = ""
        Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'btnValPhNmb
        '
        Me.btnValPhNmb.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnValPhNmb.Location = New System.Drawing.Point(88, 104)
        Me.btnValPhNmb.Name = "btnValPhNmb"
        Me.btnValPhNmb.Size = New System.Drawing.Size(120, 32)
        Me.btnValPhNmb.TabIndex = 2
        Me.btnValPhNmb.Text = "Validate Phone Number"
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.InactiveCaptionText
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.btnValPhNmb)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "frmMain"
        Me.Text = "Phone Number Validator"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub btnValPhNmb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValPhNmb.Click
        'Check length of input string: it must be exactly 12.
        'Check Substring(3,1) and Substring(7,1), i.e. the fourth and _
        'eighth character must be "-".

        Dim TextBox1 As String
              
        CheckInput() 'check to see if input is numeric and between 0 and 11


        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
    End Sub
    Public Sub CheckInput()
        If (Not IsNumeric(Me.TextBox1.Text)) Then 'checks user input for numeric and range
            MsgBox("Please enter a numeric telephone number starting with the area code.")
            TextBox1.Text = ""

        End If
    End Sub
End Class


So you can see my delima. Please someone help me code this correctly. I have tried everything I know so far with no luck. I am very green to this, in fact no previous programming experience at all. But I love a good challenge.

28-10-2005 at 02:19 AM
View Profile Send Email to User Show All Posts | Quote Reply
TJ_01
Level: VB Lord


Registered: 24-08-2005
Posts: 320
icon Re: Coding using IsNumeric

I dont have my vs.net installed right now. I got this from one of my reference cd. Check it out if it helps.

Filling Char between a string characters


Function ExplodeString(ByVal Source As String, Optional ByVal fillChar As Char = _
    " "c) As String
    Dim i As Integer
    Dim sb As New Text.StringBuilder(Source.Length * 2)

    ' exit if no character
    If Source Is Nothing OrElse Source.Length = 0 Then Return ""

    ' inserts the first char
    sb.Append(Source.Chars(0))
    ' insert the filling char before each character
    For i = 1 To Source.Length - 1
        sb.Append(fillChar)
        sb.Append(Source.Chars(i))
    Next

    Return sb.ToString

End Function



HTH

____________________________
Im JAMES  

28-10-2005 at 02:36 AM
View Profile Send Email to User Show All Posts | Quote Reply
Chris_871
Level: Master


Registered: 30-11-2002
Posts: 106
icon Re: Coding using IsNumeric

Hi

You can also use this piece of code, which may fulfill your requirement.

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim cc As String
        cc = txttelephone.Text.Replace("-", "")
        If Val(cc).ToString("###-###-####") = txttelephone.Text Then
            MsgBox("Valid format")
        Else
            MsgBox("Invalid phone number format please check")
        End If
End Sub

Regards
Chris

28-10-2005 at 09:47 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Coding using IsNumeric Solved Topic
Previous Topic (Adding Papers to Printers.)Next Topic (about pointing record in a datagrid) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder