Restritcing an Input's box input to Numbers only

gxs
gxs used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?
Use this:

Dim Hours As Integer 
Dim strTmp as String

strTmp = 0 'set other default value
Do
strTmp = InputBox("Please Enter Hours Worked",,strTmp)
if strTmp = "" Then
'user selected cancel
Exit sub
End If
If Not IsNumeric(strTmp) Then
MsgBox("You have to specify a numeric value")
Else
Hours = CInt(strTmp)
Exit Do
End If
Loop

Open in new window


Change the code for Payrate in the same way

Commented:
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

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

LazyFolk,

This will throw an exception on line Hours = CInt(InputBox("Please Enter Hours Worked"))

Commented:
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

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial