12String Level: Protégé

 Registered: 28-10-2005 Posts: 4
|
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.
|