Open the Validate Number Solution.sln file contained in the VB2017\\Chap07\\Vali
ID: 3911170 • Letter: O
Question
Open the Validate Number Solution.sln file contained in the VB2017Chap07Validate Number Solution folder. The interface provides a text box for entering a 9-digit number. The btnValidate_Click procedure should use the algorithm and example shown in Figure 7-56 to validate the user’s entry. The procedure should display a message indicating whether the entry is or is not valid. Code the procedure. Include any other code that will professionalize the interface. Save the solution and then start and test the application. Create an independent function to validate the number, pass the number as a string to the function, and have the function return a boolean (true if valid, false if invalid). Before invoking the function to validate the number, insure that the input text box contains the correct number of characters and that all the characters are numbers, and if not, display a message box informing the user to enter the correct number of characters. If the number is valid, display "Valid!" in the output label. If the number is not valid, display in red text "Not Valid!". Keep the TextAlign property of the output label MiddleCenter.
Algorithm 1. Starting with the second digit, multiply every other digit by 2. (These will be the 2. If a product from Step 1 is greater than 9, sum the two digits in the product. For 3. Add together the results from Steps 1 and 2 and each of the digits skipped in Step 1 4. Divide the sum from Step 3 by 10 and find the remainder. If the remainder is 0, then second, fourth, sixth, and eighth digits.) example, if the product is 12, add the 1 to the 2, giving 3. (The skipped digits will be the first, third, fifth, seventh, and ninth digits.) the number is valid Number: 631620176 Step 1: 631 6 12 1+4 5 Step 2: Step 3: 6 61 Step 4: 12 3 6 30 30 Mod 10Explanation / Answer
ScreenShot
-------------------------------------------------------------------------
Program
'Form name=frmValidate , Textbox name=txtNumber , button name=btnValidate and result label name=lblValidNumber
Public Class frmValidate
'Validate button click
Private Sub btnValidate_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
'Check the entred string's length and is digit or not,if condition false generate error message otherwise goto next step
If Len(Trim(txtNumber.Text)) <> 9 Or IsNumeric(txtNumber.Text) = False Then
MsgBox("Please enter a Number with 9 characters.")
txtNumber.Clear()
txtNumber.Focus()
Exit Sub
End If
'call isValid function to check the entered data correct or not
If isValid(txtNumber.Text) Then
lblValidNumber.Text = "Valid!"
lblValidNumber.ForeColor = Color.Black
Else
lblValidNumber.Text = "Not Valid!"
lblValidNumber.ForeColor = Color.Red
End If
End Sub
'Function to check the entered data validity
Private Function isValid(strNumber As String) As Boolean
'Variables for looping , tempstrring generation and total sum calculation
Dim I As Integer
Dim intTemp As Integer
Dim strTemp As String
Dim intTot As Integer
'loop to get each character of the string
For I = 1 To Len(strNumber)
'take even index numbers
If I Mod 2 = 0 Then
'convert into integer for multiplication
intTemp = Integer.Parse(Mid(strNumber, I, 1)) * 2
'if result of multiplication>9 then add digits and store
If intTemp > 9 Then
strTemp = intTemp.ToString
intTemp = Integer.Parse(Mid(strNumber, 1, 1)) + Integer.Parse(Mid(strNumber, 2, 1))
End If
'if odd index characters convert as integer without multiplication
Else
intTemp = Integer.Parse(Mid(strNumber, I, 1))
End If
'Calculate sum of all digits
intTot += intTemp
Next I
'Last step,find mod of sum with 10 division,if result=0 then valid otherwise not valid
If intTot Mod 10 = 0 Then
isValid = True
Else
isValid = False
End If
End Function
End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.