[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 830
  • Last Modified:

check for numeric value function (easy point)

hi there,
i need to create a function that will take the value of a text box and check if it is a numeric field or not.
if it is number then no problem, but if on the other hand is text then warm the user.
i have something like this but it gives me the message box twice?
any help would be appreciated.

 Public Shared Function NumberField(ByVal Extension As String) As String

        Dim number As String
        If IsNumeric(Extension) = True Then
            number = Extension
        Else
            number = MsgBox("you must enter a numeric value.", "Entry Error")
        End If
        Return number
    End Function
0
jsctechy
Asked:
jsctechy
1 Solution
 
Arthur_WoodCommented:
try this change:

Public Shared Function NumberField(ByVal Extension As String) As String

        Dim number As String
        If IsNumeric(Extension) = True Then
            number = Extension
        Else
            MessageBox.Show("You must enter a numeric value.")
            number = "-999"
        End If
        Return number
    End Function
 
Authored by: jsctechy
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"but it gives me the message box twice?"

I don't see any reason why you would get two MsgBoxes...

Perhaps you have used AddHandler() AND ALSO have a Handles clause on the same sub?
0
 
jsctechyAuthor Commented:
i'm still getting the message box twice here is how i call it:

If Tools.NumberField(txtExtension.Text) = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jsctechyAuthor Commented:
actually i'm calling the function twice to validate the same number so how can i do to by pass this error and check the number only once?
do i have to assign it to a value and use that instead?
If Tools.NumberField(txtExtension.Text) = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If

        'Search for Dates and extension
        If Tools.NumberField(txtExtension.Text) <> "" Then
            If cboBranch.Text = "" Then
                If txtAreaCode.Text = "" Then
                    If txtExchange.Text = "" Then
                        If txtNumber.Text = "" Then
                            MsgBox("You are looking for Dates and extension only", MsgBoxStyle.Information, "Date & Extension Search Only")
                        End If
                    End If
                End If
            End If
        End If
0
 
jsctechyAuthor Commented:
ok i fix it like this:
        Dim Extension As String
        Extension = Tools.NumberField(txtExtension.Text)

        If Extension = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If

        'Search for Dates and extension
        If Extension <> "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates and extension only", MsgBoxStyle.Information, "Date & Extension Search Only")
        End If

but i want that if is not a numeric value then don't run the message box at all, how do i do this? and instead go back to the text box and set the focus and dont let you moved from there untill you have satisfied the requiered

Thanks
0
 
AutoeformsCommented:
i could be way off base here but looking out from a different angle. why do you need the error. make your life easy by only allowing them to enter numbers.

 use the masked edit control
or add the following to your key press event to you text box

        If IsNumeric(e.KeyChar) = False Then
            e.Handled = True
        End If

      or e.handled = ( e.Handled = True)
   
good luck
greg

0
 
Arthur_WoodCommented:
this code:

If Tools.NumberField(txtExtension.Text) = "" Then
        ElseIf cboBranch.Text = "" Then
        ElseIf txtAreaCode.Text = "" Then
        ElseIf txtExchange.Text = "" Then
        ElseIf txtNumber.Text = "" Then
            MsgBox("You are looking for Dates only", MsgBoxStyle.Information, "Date Search Only")
        End If

will generate two message boxesif the txtExtension is not a number, and here is how:

The first time come from the NumberField function, since the field is not a number, and you code, as wriiten (not my chane), will generate the MsgBox, and return a "0" - the Value of the MsgBox function (yes, MsgBox is a FUNCTION, and it returns the value of the Key that the user clicked, to close the MsgBox that was displayed), so then in the above code, the initial test (which presented the first messgeBox), fails (ince the value returned by your function is NOT "", and then, if each of the other tests faile (becuase those fields are NOT ""), then the second messagebox will be displayed.

Set a Breakpoint on the first line

If Tools.NumberField(txtExtension.Text) = "" Then
 

and then single-step through the code to see what is actually occuring, and why you get two message boxes.

AW
0
 
jsctechyAuthor Commented:
here is how i solved this issue:

 Private Sub txtPhone_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPhone.Validated
        If IsNumeric(txtPhone.Text) = True Then
        ElseIf txtPhone.Text = "" Then
            'nothing
        Else
            MessageBox.Show("You must enter a numeric value.", "Phone # Data Error")
            txtPhone.Focus()
        End If
    End Sub

Thanks for all your comments
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now