viusal basic input validation integer - basic.visual

This is a discussion on viusal basic input validation integer - basic.visual ; 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 ...

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 28

viusal basic input validation integer

  1. Default 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



  2. Default Re: viusal basic input validation integer

    code_wrong wrote:

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


    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!!!

  3. Default Re: viusal basic input validation integer


    "Phlip" <phlip2005@gmail.com> wrote in message
    news:ia2Nf.17971$NS6.5433@newssvr30.news.prodigy.com...
    > code_wrong wrote:
    >
    >> Visual Basic (not dot net)
    >> what is the best way to check the User has entered an integer into an
    >> InputBox?

    >
    > 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.


    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



  4. Default Re: 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?


    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(Value 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$(1000, "#,###"), 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



  5. Default Re: viusal basic input validation integer

    code_wrong wrote:
    > 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
    >
    >


    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



  6. Default Re: viusal basic input validation integer

    > 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.


    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



  7. Default Re: viusal basic input validation integer

    Rick Rothstein [MVP - Visual Basic] wrote:
    >>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.

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


    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


  8. Default Re: viusal basic input validation integer

    Rick ...

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

    --

    Randy Birch
    MS MVP Visual Basic
    http://vbnet.mvps.org/

    Please reply to the newsgroups so all can participate.




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


    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(Value 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$(1000, "#,###"), 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



  9. Default Re: viusal basic input validation integer


    "code_wrong" <tac@tac.co.uk> wrote in message
    news:4404ac4c$1_3@mk-nntp-2.news.uk.tiscali.com...
    > 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?
    >


    "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(sInput)
    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



  10. Default Re: viusal basic input validation integer

    Steve Gerrard wrote:

    > MsgBox "How is " & sInput & " an integer?"


    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!!!

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast

Similar Threads

  1. Date input validation
    By Application Development in forum TCL
    Replies: 4
    Last Post: 09-14-2007, 04:54 PM
  2. How to display closed caption in Viusal Basic
    By Application Development in forum basic.visual
    Replies: 0
    Last Post: 03-11-2005, 10:38 AM
  3. How to display closed caption in Viusal Basic
    By Application Development in forum basic.visual
    Replies: 2
    Last Post: 03-11-2005, 10:36 AM
  4. How to display closed caption in Viusal Basic
    By Application Development in forum basic.visual
    Replies: 0
    Last Post: 03-04-2005, 11:07 AM
  5. java.io.IOException: DER input, Integer tag error
    By Application Development in forum Java
    Replies: 1
    Last Post: 12-07-2004, 01:42 AM