Link to home
Start Free TrialLog in
Avatar of gxs
gxsFlag 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,

Avatar of andygrant2005
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Avatar of smilitaru
Flag of Romania image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cant you simply keep asking for Hours until a numeric value is entered ?

It would be something like this (untested) :

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

Open in new window


This will throw an exception on line Hours = CInt(InputBox("Please Enter Hours Worked"))
Thats true because of the cInt

the correct code would be

Dim Hours

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

Open in new window

Avatar of 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