Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i see if there is any integer in a textbox?

Status
Not open for further replies.

kruxty

Programmer
Jul 17, 2001
197
0
0
PT
I have a textbox, and i want to see if there is any integer in this textbox! How can i do this?
By the way there are any function that verifys the symbols that can't be written in a name of a file such as (,;:) ?
 
To check for an integer you can use:

If CStr(Cint(textbox))=textbox Then
'integer in textbox
End If

Chris.
 
This question apparently is a class assignment. It has been asked here a number of times.
BTW.
If CStr(Cint(textbox))=textbox Then
'integer in textbox
End If
Will "blow the program" if "A" or > 32768 is entered.
Are negative numbers allowed?
How about leading zeroes which will fail the above test because 008 <> 8 in a string compare? Compare Code (Text)
Generate Sort in VB or VBScript
 
Krusty,

Drop this code snippet into a class module and your problem is solved.

Public Function Tester(vData As String) As Boolean
Dim iTrac As String
Dim i As Integer
Dim iLen As Integer
Dim iPos As Integer

iLen = Len(vData)
iPos = 1

Do While i < iLen

iTrac = Mid(vData, iPos, 1)
If iTrac Like &quot;[0-9]&quot; Then
MsgBox &quot;Found integer in testbox&quot;
Tester=True ' integer found
Exit Do
Else
iPos = iPos + 1 'increment position
i = i + 1 'increment loop counter
End If
Loop



End Function
 
That fails when length <= 1. It also fails if &quot;1A&quot; entered. You should test these things.
Try
Private Sub Command1_Click()
Dim b As Boolean
b = Tester(Text1(0).Text)
MsgBox CStr(b)
End Sub
Public Function Tester(vData As String) As Boolean
Dim I As Integer
Dim J As Integer
J = Len(vData)
If J > 0 Then
Tester = True
For I = 1 To J
If Not (Mid$(vData, I, 1) Like &quot;[0-9]&quot;) Then
Tester = False ' not a positive integer
Exit For
End If
Next
End If
End Function Compare Code (Text)
Generate Sort in VB or VBScript
 
It is often easier to work this from the other side. By this I mean to look for for ways to control the data instead of trying to check it after the fact. FAQ222-45 for instance shows how to limit the data going into the text box.

Sometimes this just isn't reasonable, especially when you want to allow users to enter non numeric data. For this, it appears your only recourse is to review the data. There's enough advice above to get you going in that direction. FAQ222-302 might help some too, at least for finding a place to put your validation code.

Wil Mead
wmead@optonline.net

 
I do not know of a function which verifies allowable symbols in filenames. I usually just use the KeyPress event for each textbox to limit what my users can enter into text boxes.

Private Sub Text1_KeyPress(KeyAscii As Integer)
'use the ascii chart to determine which
'keys the user can and can't press
'when they press an invalid key set the value
'of the key they pressed to 0 (null) and
'the key pressed will not be entered into the
'textbox.

Select Case KeyAscii
Case 1 To 47
KeyAscii = 0
End Select

End Sub

scott
 
JohnYingling,
Here's something off the top of my head - maybe it'll work :) Sorry about the nested ifs.. it's late :p

bolInputIsInteger = False

If IsNumeric(txtInput.Value) Then
If Fix(CDbl(txtInput.Value) >= -32,768 And _
Fix(CDbl(txtInput.Value) <= 32,767 Then
If CStr(Cint(txtInput.Value)) = txtInput.Value Then
bolInputIsInteger = True
End If
End If
End If
 
Oops.. I forgot some end parens.. all references to &quot;Fix(CDbl(txtInput.Value)&quot; should be &quot;Fix(CDbl(txtInput.Value))&quot;
 
I hate to be picky, honest, but I hate it when users &quot;catch&quot; me with a &quot;stupid&quot; problem where I can't really argue with them. &quot;Why can't I enter &quot;01&quot;?
&quot;01&quot; will produce an error with the following.
If CStr(Cint(txtInput.Value)) = txtInput.Value
The &quot;real&quot; problem with Cint is that it accepts &quot;.&quot;, &quot;-&quot;, &quot;+&quot;, leading and trailing. It is a problem of
eliminating what CInt will accept and therfore a &quot;String&quot; problem first.
Code:
Private Sub Command1_Click()
    MsgBox CStr(IsInteger(txtca(0)))
End Sub

Public Function IsInteger(valor As String) As Boolean
    ' Cint accepts Leading / Trailing spaces
    ' Cint accepts leading or Trailing Signs
    ' &quot;-1&quot;, &quot;1-&quot;, &quot; 1- &quot;
    ' &quot;-0001  &quot;
    Dim I      As Integer
    Dim blnInt As Boolean
    If InStr(1, valor, &quot;.&quot;) > 0 Then
    ElseIf InStr(1, valor, &quot;+&quot;) > 0 Then   ' No + ???
    ElseIf InStr(1, valor, &quot;-&quot;) > 0 Then   ' No - ???
    ElseIf Len(Trim$(valor)) <> Len(valor) Then ' No spaces ???
    Else
        On Error Resume Next
            I = CInt(valor) ' Final Check
            blnInt = (Err.Number = 0)
        On Error GoTo 0
    End If
    IsInteger = blnInt
End Function
Compare Code (Text)
Generate Sort in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top