Check for integer input

Author Message
sgiannop

  • Total Posts : 3
  • Scores: 0
  • Reward points : 0
  • Joined: 11/27/2011
  • Status: offline
Check for integer input Monday, November 28, 2011 7:39 AM (permalink)
0
Greetings everyone. I want to check with an if statement, if the user input is an integer or not. How do i do this? I 've tried the following:
---------------------------------------------------------------------------------------
input = InputBox("Give an integer:")
If Int(input) = input Then
MsgMox "You gave an integer."
End If
---------------------------------------------------------------------------------------
But, unfortunately this does not seem to work. Any idea? Thanks.
 
#1
    59cobalt

    • Total Posts : 969
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Check for integer input Monday, November 28, 2011 7:55 AM (permalink)
    0
    If VarType(input) = vbInteger Then MsgBox "You gave an integer."

     
    #2
      sgiannop

      • Total Posts : 3
      • Scores: 0
      • Reward points : 0
      • Joined: 11/27/2011
      • Status: offline
      Re:Check for integer input Monday, November 28, 2011 9:21 AM (permalink)
      0
      Thanks a lot! for the quick answer.
       
      59cobalt

      If VarType(input) = vbInteger Then MsgBox "You gave an integer."


       
      Though this seems to be another right way to check if the input is integer, i still don't get the right result
      if i give integer as an input. check it for yourself! Any reason?
       
      #3
        sgiannop

        • Total Posts : 3
        • Scores: 0
        • Reward points : 0
        • Joined: 11/27/2011
        • Status: offline
        Re:Check for integer input Monday, November 28, 2011 9:30 AM (permalink)
        0


        If VarType(input) = vbInteger Then MsgBox "You gave an integer."



        By the way, the reason that the above way fails is different following this way. Now if is executed even
        whem we give decimal numbers in the input. when we:


        MsgBox VarType(input)


        we take as a result the integer 8 in both cases that input is an integer or a decimal.
        What's wrong anyway?
         
        #4
          59cobalt

          • Total Posts : 969
          • Scores: 91
          • Reward points : 0
          • Joined: 7/17/2011
          • Status: offline
          Re:Check for integer input Monday, November 28, 2011 10:54 AM (permalink)
          0
          That's because InputBox() always returns either a string or Empty. You can use IsNumeric() to check if the string contains a numeric value. However, that will return True for doubles as well. Try this:
          If IsNumeric(input) Then
           If CStr(CInt(input)) = input Then
           WScript.Echo "Input is an Integer."
           ElseIf CStr(CLng(input)) = input Then
           WScript.Echo "Input is a Long."
           ElseIf CStr(CDbl(input)) = input Then
           WScript.Echo "Input is a Double."
           End If
          End If

           
          #5
            ehvbs

            • Total Posts : 3320
            • Scores: 112
            • Reward points : 0
            • Joined: 6/22/2005
            • Location: Germany
            • Status: offline
            Re:Check for integer input Monday, November 28, 2011 10:55 AM (permalink)
            0
            Using InputBox() as your GUI is a bad idea. If you insist on communicating with your user via vanishing popups, you you should be aware of:
            • InputBox() returns a string (vbString == 8) or an empty value (vbEmpty == 0) on abort
            • the IsNumeric() functions checks whether a string can be converted to a value of numerical (sub) type; this conversion may loose info
            • a numerical string can be converted to a double (CDbl()), a double may have no fractional part (double == Fix(double))
            • a fixed double can be converted to a integral (sub) type: CByte(), CInt(), CLng() - if it is in the appropriate range
             
            #6
              ebgreen

              • Total Posts : 8219
              • Scores: 98
              • Reward points : 0
              • Joined: 7/12/2005
              • Status: offline
              Re:Check for integer input Tuesday, November 29, 2011 3:48 AM (permalink)
              0
              Here is a quick and dirty function that should handle most cases.
               
               
              Function IsInt(strIn)
              Dim strFractional
              if Not IsNumeric(strIn) Then
              IsInt = False
              Exit Function
              End If
              if InStr(strIn, ".") > 0 Then
              strFractional = Split(strIn, ".")(1)
              if CInt(strFractional) > 0 Then
              IsInt = False
              Exit Function
              End If
              End If
              IsInt = True
              End Function

              "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
              Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
              http://www.visualbasicscript.com/m_47117/tm.htm
               
              #7

                Online Bookmarks Sharing: Share/Bookmark

                Jump to:

                Current active users

                There are 0 members and 1 guests.

                Icon Legend and Permission

                • New Messages
                • No New Messages
                • Hot Topic w/ New Messages
                • Hot Topic w/o New Messages
                • Locked w/ New Messages
                • Locked w/o New Messages
                • Read Message
                • Post New Thread
                • Reply to message
                • Post New Poll
                • Submit Vote
                • Post reward post
                • Delete my own posts
                • Delete my own threads
                • Rate post

                2000-2012 ASPPlayground.NET Forum Version 3.9