Avatar of gxs
gxs
Flag for United States of America asked on

Restritcing an Input's box input to Numbers only

Hello everyone,

I'm trying to restrict input box entries to numbers only.

I know the method IsNumeric but it doesn't work with me because I assigned the input's box value to a variable. So the whole thing is getting a little complicated.

Take a look at my code and you'll understand what I mean:

-------------------------------------------------------------------------------------
'Salary Calculator
Option Strict On

Public Class SalaryCal

    Private Sub BtnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCal.Click

        Dim Salary As Integer = 0
        Dim Name As String = InputBox("Please Enter Your name")
        Dim Hours As Integer = CInt(InputBox("Please Enter Hours Worked"))
        Dim Payrate As Integer = CInt(InputBox("Please Enter your hourly payrate"))

        Dim NetSalary As Integer = 0

        If Hours <= 40 Then
            Salary = Hours * Payrate
        ElseIf Hours > 40 Then
            Salary = CInt((40 * Payrate) + (Payrate * (Hours - 40) * 1.5))
        End If

        If Salary > 2000 Then
            NetSalary = CInt((Salary / 100) * 85)
            TxtResult.Text &= Name & ", " & "Your NetSalary is " & FormatCurrency(NetSalary) & " and your Salary is " & (Salary) & ". " & " You have been taxed %15" & vbNewLine

        ElseIf Salary <= 2000 Then
            NetSalary = CInt((Salary / 100) * 90)
            TxtResult.Text &= Name & ", " & "Your NetSalary is " & FormatCurrency(NetSalary) & " and your Salary is " & (Salary) & ". " & " You have been taxed %10" & vbNewLine
        End If

    End Sub
End Class
-------------------------------------------------------------------------------------

The bolded codes need to be modified a bit so Hour, Payrate variables accept Numeric input from the input box only and the Name variable accept Characters only.

Waiting for you help plus appreciated and thanks in advance,

Mike.
Visual Basic.NET.NET ProgrammingMicrosoft Development

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
andygrant2005

You could wrap the input box in a val statement.  This will return 0 if any alpha characters are entered.

Dim Hours As Integer = CInt(Val(InputBox("Please Enter Hours Worked")))
Dim Payrate As Integer = CInt(Val(InputBox("Please Enter your hourly payrate")))

This will return zero if non numeric characters are entered.

You can then check for zero if you want to throw your own error message.

Will this help?
ASKER CERTIFIED SOLUTION
smilitaru

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
LazyFolk

Cant you simply keep asking for Hours until a numeric value is entered ?

It would be something like this (untested) :

Dim Hours As Integer
...
Do
      Hours = CInt(InputBox("Please Enter Hours Worked"))
Loop while NOT IsNumeric(Hours)
...

Open in new window

smilitaru

LazyFolk,

This will throw an exception on line Hours = CInt(InputBox("Please Enter Hours Worked"))
Your help has saved me hundreds of hours of internet surfing.
fblack61
LazyFolk

Thats true because of the cInt

the correct code would be

Dim Hours

Do
    Hours = InputBox("Please Enter Hours Worked")
Loop While Not IsNumeric(Hours)

Open in new window

kaufmed

Another option, albeit slightly less readable:

Dim Hours As Integer

While Not Integer.TryParse(InputBox("Please Enter Hours Worked"), Hours)
End While

Open in new window