viusal basic input validation integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • code_wrong

    #1

    viusal basic input validation integer

    Visual Basic (not dot net)
    what is the best way to check the User has entered an integer into an
    InputBox?

    isNumeric() checks for a numeric value .. but does not notify of numbers
    with decimal places
    inputBox returns a string so I could check for decimal point??? this seems
    like overkill

    The value returned can be asigned into an Integer type and then a Single
    type ... the two can be compared .. but still this does not complain about
    numbers with decimal places if the fractional part is zero

    what is the simple solution?

    cw


  • Phlip

    #2
    Re: viusal basic input validation integer

    code_wrong wrote:
    [color=blue]
    > Visual Basic (not dot net)
    > what is the best way to check the User has entered an integer into an
    > InputBox?[/color]

    Use a masked input box that rejects invalid entries at the keystroke.

    Look up "masked input" - there were lots of options for VB6. 6 years ago.

    And upgrade to a dynamic OO language as soon as you can. VB6 will bring you
    down.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

    Comment

    • code_wrong

      #3
      Re: viusal basic input validation integer


      "Phlip" <phlip2005@gmai l.com> wrote in message
      news:ia2Nf.1797 1$NS6.5433@news svr30.news.prod igy.com...[color=blue]
      > code_wrong wrote:
      >[color=green]
      >> Visual Basic (not dot net)
      >> what is the best way to check the User has entered an integer into an
      >> InputBox?[/color]
      >
      > Use a masked input box that rejects invalid entries at the keystroke.
      >
      > Look up "masked input" - there were lots of options for VB6. 6 years ago.
      >
      > And upgrade to a dynamic OO language as soon as you can. VB6 will bring
      > you
      > down.[/color]

      unfortunately I am restricted to using inputbox() ... why? .. if I change
      the method for input now, the students will likely will be traumatised ..
      these are very early days


      Comment

      • Rick Rothstein [MVP - Visual Basic]

        #4
        Re: viusal basic input validation integer

        > Visual Basic (not dot net)[color=blue]
        > what is the best way to check the User has entered an integer into an
        > InputBox?[/color]

        Here are two functions that I have posted in the past for similar
        questions..... one is for digits only and the other is for "regular"
        numbers:

        Function IsDigitsOnly(Va lue As String) As Boolean
        IsDigitsOnly = Len(Value) > 0 And _
        Not Value Like "*[!0-9]*"
        End Function

        Function IsNumber(ByVal Value As String) As Boolean
        ' Leave the next statement out if you don't
        ' want to provide for plus/minus signs
        If Value Like "[+-]*" Then Value = Mid$(Value, 2)
        IsNumber = Not Value Like "*[!0-9.]*" And _
        Not Value Like "*.*.*" And _
        Len(Value) > 0 And Value <> "." And _
        Value <> vbNullString
        End Function

        Here are revisions to the above functions that deal with the local settings
        for decimal points (and thousand's separators) that are different than used
        in the US (this code works in the US too, of course).

        Function IsNumber(ByVal Value As String) As Boolean
        Dim DP As String
        ' Get local setting for decimal point
        DP = Format$(0, ".")
        ' Leave the next statement out if you don't
        ' want to provide for plus/minus signs
        If Value Like "[+-]*" Then Value = Mid$(Value, 2)
        IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _
        Not Value Like "*" & DP & "*" & DP & "*" And _
        Len(Value) > 0 And Value <> DP And _
        Value <> vbNullString
        End Function

        I'm not as concerned by the rejection of entries that include one or more
        thousand's separators, but we can handle this if we don't insist on the
        thousand's separator being located in the correct positions (in other words,
        we'll allow the user to include them for their own purposes... we'll just
        tolerate their presence).

        Function IsNumber(ByVal Value As String) As Boolean
        Dim DP As String
        Dim TS As String
        ' Get local setting for decimal point
        DP = Format$(0, ".")
        ' Get local setting for thousand's separator
        ' and eliminate them. Remove the next two lines
        ' if you don't want your users being able to
        ' type in the thousands separator at all.
        TS = Mid$(Format$(10 00, "#,###"), 2, 1)
        Value = Replace$(Value, TS, "")
        ' Leave the next statement out if you don't
        ' want to provide for plus/minus signs
        If Value Like "[+-]*" Then Value = Mid$(Value, 2)
        IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _
        Not Value Like "*" & DP & "*" & DP & "*" And _
        Len(Value) > 0 And Value <> DP And _
        Value <> vbNullString
        End Function

        Rick


        Comment

        • Duane Arnold

          #5
          Re: viusal basic input validation integer

          code_wrong wrote:[color=blue]
          > Visual Basic (not dot net)
          > what is the best way to check the User has entered an integer into an
          > InputBox?
          >
          > isNumeric() checks for a numeric value .. but does not notify of numbers
          > with decimal places
          > inputBox returns a string so I could check for decimal point??? this seems
          > like overkill
          >
          > The value returned can be asigned into an Integer type and then a Single
          > type ... the two can be compared .. but still this does not complain about
          > numbers with decimal places if the fractional part is zero
          >
          > what is the simple solution?
          >
          > cw
          >
          >[/color]

          What you can do on a text box control that is being used for only
          numbers is use the Keypress Event and check the key being pressed. If
          it's a numeric key 0-9, you accept the keypress. If the key is not a
          numeric key 0-9, the you cancel or don't accept the keypress. I recall
          ASCII code 0 or something like that cancels key-code from a key press so
          that it's not accepted.

          You just write a Keypress event routine that accepts numeric keys or
          possibly a control key like the ESC-Key, Backspace-Key or ( Tab-key set
          focus to the next control), take the appropriate actions on the
          control-keys, cancel any key that's not 0-9 and only accept the 0-9 keys.

          Duane :)


          Comment

          • Rick Rothstein [MVP - Visual Basic]

            #6
            Re: viusal basic input validation integer

            > What you can do on a text box control that is being used for only[color=blue]
            > numbers is use the Keypress Event and check the key being pressed. If
            > it's a numeric key 0-9, you accept the keypress. If the key is not a
            > numeric key 0-9, the you cancel or don't accept the keypress. I recall
            > ASCII code 0 or something like that cancels key-code from a key press so
            > that it's not accepted.
            >
            > You just write a Keypress event routine that accepts numeric keys or
            > possibly a control key like the ESC-Key, Backspace-Key or ( Tab-key set
            > focus to the next control), take the appropriate actions on the
            > control-keys, cancel any key that's not 0-9 and only accept the 0-9 keys.[/color]

            You can't take appropriate action on "true" control keys in the KeyPress
            event; that requires the KeyUp or KeyDown event. And if you don't handle all
            of the keystrokes that allow a user to Paste data (and the Mouse events that
            allow it too), then your user will be able to Paste non-digits into the
            TextBox. And, after all of that, I think you will still have missed one or
            two places where the user can initiate a Paste operation.

            Rick


            Comment

            • Duane Arnold

              #7
              Re: viusal basic input validation integer

              Rick Rothstein [MVP - Visual Basic] wrote:[color=blue][color=green]
              >>What you can do on a text box control that is being used for only
              >>numbers is use the Keypress Event and check the key being pressed. If
              >>it's a numeric key 0-9, you accept the keypress. If the key is not a
              >>numeric key 0-9, the you cancel or don't accept the keypress. I recall
              >>ASCII code 0 or something like that cancels key-code from a key press so
              >>that it's not accepted.
              >>
              >>You just write a Keypress event routine that accepts numeric keys or
              >>possibly a control key like the ESC-Key, Backspace-Key or ( Tab-key set
              >>focus to the next control), take the appropriate actions on the
              >>control-keys, cancel any key that's not 0-9 and only accept the 0-9 keys.[/color]
              >
              >
              > You can't take appropriate action on "true" control keys in the KeyPress
              > event; that requires the KeyUp or KeyDown event. And if you don't handle all
              > of the keystrokes that allow a user to Paste data (and the Mouse events that
              > allow it too), then your user will be able to Paste non-digits into the
              > TextBox. And, after all of that, I think you will still have missed one or
              > two places where the user can initiate a Paste operation.
              >
              > Rick
              >
              >[/color]

              You may be right I don't recall. However, I have used Keyup, Keydown,
              Keypress and Mouse events. In the real world or in accounting
              appliactions in business solutions in corporations where I have applied
              the numeric only solution, it's worked like a champ on pure data entry
              applications where the numbers had to be banged in - there is no cutting
              and pasting.

              It's something the poster is going to have to figure out for his or
              herself as to the best approach.

              Duane :)

              Comment

              • Randy Birch

                #8
                Re: viusal basic input validation integer

                Rick ...

                Is not: Not Value Like "*[!0-9]*" ... redundant?

                --

                Randy Birch
                MS MVP Visual Basic


                Please reply to the newsgroups so all can participate.




                "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews @NOSPAMcomcast. net>
                wrote in message news:-cSdnbZbLfZ3JZnZ nZ2dnUVZ_tGdnZ2 d@comcast.com.. .[color=blue]
                > Visual Basic (not dot net)
                > what is the best way to check the User has entered an integer into an
                > InputBox?[/color]

                Here are two functions that I have posted in the past for similar
                questions..... one is for digits only and the other is for "regular"
                numbers:

                Function IsDigitsOnly(Va lue As String) As Boolean
                IsDigitsOnly = Len(Value) > 0 And _
                Not Value Like "*[!0-9]*"
                End Function

                Function IsNumber(ByVal Value As String) As Boolean
                ' Leave the next statement out if you don't
                ' want to provide for plus/minus signs
                If Value Like "[+-]*" Then Value = Mid$(Value, 2)
                IsNumber = Not Value Like "*[!0-9.]*" And _
                Not Value Like "*.*.*" And _
                Len(Value) > 0 And Value <> "." And _
                Value <> vbNullString
                End Function

                Here are revisions to the above functions that deal with the local settings
                for decimal points (and thousand's separators) that are different than used
                in the US (this code works in the US too, of course).

                Function IsNumber(ByVal Value As String) As Boolean
                Dim DP As String
                ' Get local setting for decimal point
                DP = Format$(0, ".")
                ' Leave the next statement out if you don't
                ' want to provide for plus/minus signs
                If Value Like "[+-]*" Then Value = Mid$(Value, 2)
                IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _
                Not Value Like "*" & DP & "*" & DP & "*" And _
                Len(Value) > 0 And Value <> DP And _
                Value <> vbNullString
                End Function

                I'm not as concerned by the rejection of entries that include one or more
                thousand's separators, but we can handle this if we don't insist on the
                thousand's separator being located in the correct positions (in other words,
                we'll allow the user to include them for their own purposes... we'll just
                tolerate their presence).

                Function IsNumber(ByVal Value As String) As Boolean
                Dim DP As String
                Dim TS As String
                ' Get local setting for decimal point
                DP = Format$(0, ".")
                ' Get local setting for thousand's separator
                ' and eliminate them. Remove the next two lines
                ' if you don't want your users being able to
                ' type in the thousands separator at all.
                TS = Mid$(Format$(10 00, "#,###"), 2, 1)
                Value = Replace$(Value, TS, "")
                ' Leave the next statement out if you don't
                ' want to provide for plus/minus signs
                If Value Like "[+-]*" Then Value = Mid$(Value, 2)
                IsNumber = Not Value Like "*[!0-9" & DP & "]*" And _
                Not Value Like "*" & DP & "*" & DP & "*" And _
                Len(Value) > 0 And Value <> DP And _
                Value <> vbNullString
                End Function

                Rick


                Comment

                • Steve Gerrard

                  #9
                  Re: viusal basic input validation integer


                  "code_wrong " <tac@tac.co.u k> wrote in message
                  news:4404ac4c$1 _3@mk-nntp-2.news.uk.tisca li.com...[color=blue]
                  > Visual Basic (not dot net)
                  > what is the best way to check the User has entered an integer into an
                  > InputBox?
                  >
                  > isNumeric() checks for a numeric value .. but does not notify of numbers with
                  > decimal places
                  > inputBox returns a string so I could check for decimal point??? this seems
                  > like overkill
                  >
                  > The value returned can be asigned into an Integer type and then a Single type
                  > ... the two can be compared .. but still this does not complain about numbers
                  > with decimal places if the fractional part is zero
                  >
                  > what is the simple solution?
                  >[/color]

                  "simple solution" is always a relative term in this NG :)

                  For integers, I would do this test, which deals with fractional entries as well:

                  Private Sub Command1_Click( )
                  Dim A As Single
                  Dim B As Long
                  Dim C As Single
                  Dim bNum As Boolean
                  Dim sInput As String

                  sInput = InputBox("enter an integer", , "0")
                  bNum = IsNumeric(sInpu t)
                  If bNum Then
                  A = CSng(sInput)
                  B = CLng(A)
                  C = CSng(B)
                  End If
                  If bNum And C = A Then
                  MsgBox "good dog, you entered " & B
                  Else
                  MsgBox "How is " & sInput & " an integer?"
                  End If

                  End Sub

                  That way, the user can enter 3E7 if they want, a perfectly valid way of entering
                  30,000,000 :)


                  Comment

                  • Phlip

                    #10
                    Re: viusal basic input validation integer

                    Steve Gerrard wrote:
                    [color=blue]
                    > MsgBox "How is " & sInput & " an integer?"[/color]

                    As a user, seeing sophomoric crap like that get through gives me a very low
                    opinion of the programmer on the other side.

                    For the OP; handle errors at keystroke time or at turn-around time (when the
                    field blurs, or when the current page submits). Then if the error is
                    complex, write text into a special field on the form. If the error is
                    simple, beep and turn its field red.

                    Contact with a user should be as sensitive and gentle as possible, to avoid
                    looking like the average VB interface.

                    --
                    Phlip
                    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

                    Comment

                    • Rick Rothstein [MVP - Visual Basic]

                      #11
                      Re: viusal basic input validation integer

                      > Is not: Not Value Like "*[!0-9]*" ... redundant?

                      No, it is not redundant. You can't test directly like this

                      Value Like "*[!0-9]*"

                      because that would match any **single** digit, no matter where it is located
                      and no matter what the other characters are (digits or not). The only way to
                      handle this, as you found out, is with the "double negative" approach. For a
                      string to be composed of all digits, no one of them can be a non-digit. So
                      we check for that non-digit-ness. And if it is False, then the string is
                      composed of only digits characters. But we don't want to return False
                      through the function, so we NOT the expression to turn False into True for
                      return through the function. As for syntax, the exclamation mark within the
                      squared brackets says "match all characters **except** for those that
                      follow".


                      Comment

                      • CBFalconer

                        #12
                        Re: viusal basic input validation integer

                        Phlip wrote:[color=blue]
                        > Steve Gerrard wrote:
                        >[color=green]
                        >> MsgBox "How is " & sInput & " an integer?"[/color]
                        >
                        > As a user, seeing sophomoric crap like that get through gives me
                        > a very low opinion of the programmer on the other side.[/color]

                        He didn't. That stuff is some fabrication of your newsreader. The
                        Â characters don't exist in his original.

                        --
                        "If you want to post a followup via groups.google.c om, don't use
                        the broken "Reply" link at the bottom of the article. Click on
                        "show options" at the top of the article, then click on the
                        "Reply" at the bottom of the article headers." - Keith Thompson
                        More details at: <http://cfaj.freeshell. org/google/>
                        Also see <http://www.safalra.com/special/googlegroupsrep ly/>

                        Comment

                        • Steve Gerrard

                          #13
                          Re: viusal basic input validation integer


                          "Phlip" <phlip2005@gmai l.com> wrote in message
                          news:Z29Nf.1801 7$NS6.7159@news svr30.news.prod igy.com...
                          [color=blue]
                          > For the OP; handle errors at keystroke time or at turn-around time (when the
                          > field blurs, or when the current page submits). Then if the error is
                          > complex, write text into a special field on the form. If the error is
                          > simple, beep and turn its field red.[/color]

                          OP's question was:
                          "what is the best way to check the User has entered an integer into an
                          InputBox?"

                          Followed with a second post stating:
                          "unfortunat ely I am restricted to using inputbox() ... "
                          [color=blue][color=green]
                          >> MsgBox "How is " & sInput & " an integer?"[/color]
                          >
                          > As a user, seeing sophomoric crap like that get through gives me a very low
                          > opinion of the programmer on the other side.
                          >
                          > Contact with a user should be as sensitive and gentle as possible, to avoid
                          > looking like the average VB interface.
                          >[/color]

                          Contact with a NG should be as sensitive and gentle as possible, to avoid
                          looking like you can't tell the difference between the serious code and the lame
                          NG humor :)



                          Comment

                          • Raoul Watson

                            #14
                            Re: viusal basic input validation integer


                            "code_wrong " <tac@tac.co.u k> wrote in message
                            news:4404b324$1 _1@mk-nntp-2.news.uk.tisca li.com...[color=blue]
                            >
                            > unfortunately I am restricted to using inputbox() ... why? .. if I change
                            > the method for input now, the students will likely will be traumatised ..
                            > these are very early days
                            >[/color]

                            num = VAL(Text1.text)
                            if num*10 / 10 = INT(num*10/10) then debug.print "whole number" else
                            debug.print "has decimal"



                            Comment

                            • Dean Earley

                              #15
                              Re: viusal basic input validation integer

                              CBFalconer wrote:[color=blue]
                              > Phlip wrote:[color=green]
                              >> Steve Gerrard wrote:
                              >>[color=darkred]
                              >>> MsgBox "How is " & sInput & " an integer?"[/color]
                              >> As a user, seeing sophomoric crap like that get through gives me
                              >> a very low opinion of the programmer on the other side.[/color]
                              >
                              > He didn't. That stuff is some fabrication of your newsreader. The
                              > Â characters don't exist in his original.[/color]

                              Nor in Phlips reply...
                              Phlip was referring to the content of the message itself rather than the
                              garbage characters your client seems to have added.

                              --
                              Dean Earley (dean.earley@ic ode.co.uk)
                              i-Catcher Development Team

                              iCode Systems

                              Comment

                              Working...