Checking input number 
Author Message
 Checking input number

Hello All!

I am new to VB and have a question.  Is there any built in function to
check if a input contains only number of not?

Thanks in advance



Sat, 08 Nov 2003 04:44:00 GMT  
 Checking input number
Have a look at the IsNumeric( ) function -- but, keep in mind that it allows
the following characters as well:

$  -  +  .  (   )

Here's one method of checking to see if a string contains ONLY numbers:

Public Function IsNumbersOnly(ByVal strInput As String) As Boolean
    Dim i As Integer, j As Integer
    Const strNumbers As String = "0123456789"

    For i = 1 To Len(strInput)
        If InStr(1, strNumbers, Mid$(strInput, i, 1)) = 0 Then Exit Function
    Next i

    IsNumbersOnly = True
End Function

NOTE:  This function could be made more generic by taking 2 parameters --
the "string to check" and the "filter".  That way you could do filtering on
any substring (not just numbers).

Good Luck,

Joe


Quote:
> Hello All!

> I am new to VB and have a question.  Is there any built in function to
> check if a input contains only number of not?

> Thanks in advance



Sat, 08 Nov 2003 06:12:31 GMT  
 Checking input number
IsNumeric


Quote:
> Hello All!

> I am new to VB and have a question.  Is there any built in function to
> check if a input contains only number of not?

> Thanks in advance



Sat, 08 Nov 2003 07:05:51 GMT  
 Checking input number
Sorry -- extra declaration for "j as integer" included in sample -- please
disregard.


Quote:
> Have a look at the IsNumeric( ) function -- but, keep in mind that it
allows
> the following characters as well:

> $  -  +  .  (   )

> Here's one method of checking to see if a string contains ONLY numbers:

> Public Function IsNumbersOnly(ByVal strInput As String) As Boolean
>     Dim i As Integer, j As Integer
>     Const strNumbers As String = "0123456789"

>     For i = 1 To Len(strInput)
>         If InStr(1, strNumbers, Mid$(strInput, i, 1)) = 0 Then Exit
Function
>     Next i

>     IsNumbersOnly = True
> End Function

> NOTE:  This function could be made more generic by taking 2 parameters --
> the "string to check" and the "filter".  That way you could do filtering
on
> any substring (not just numbers).

> Good Luck,

> Joe



> > Hello All!

> > I am new to VB and have a question.  Is there any built in function to
> > check if a input contains only number of not?

> > Thanks in advance



Sat, 08 Nov 2003 06:17:40 GMT  
 Checking input number

Hi,
you can use IsNumeric()

example :

                label1.caption="Hello"
                label2.caption="1234"

if IsNumeric(label1.caption) then
        msgbox "Numeric"
else
        msgbox "Non Numeric"
end if

if IsNumeric(label2.caption) then
        msgbox "Numeric"
else
        msgbox "Non Numeric"
end if

Quote:
>Hello All!

>I am new to VB and have a question.  Is there any built in function to
>check if a input contains only number of not?

>Thanks in advance



Sat, 08 Nov 2003 07:24:22 GMT  
 Checking input number
Watch out. Joe Hart's method will not work with decimal points, + - signs or
scientfic notation (for example 4.86e-9)


Sat, 08 Nov 2003 10:42:52 GMT  
 Checking input number
Some have advised you to use IsNumeric. I would like to warn you of some
potential problems that IsNumeric might cause you. Consider this

    ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws" in
what it considers a proper number and what most programmers are looking for.

You might also want to look at this short tip I had published by Pinnacle
Publishing in their Visual Basic Developer magazine. Note that your newsreader
will probably break this into two separate lines; they need to be combined into
a single line in your browser.

http://www.pinnaclepublishing.com/VB/VBmag.nsf/Index/6B5C9CBB4011F2E6...
8C31F?opendocument

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and commas
and dots -- these were meant to refer to your currency, thousands separator and
decimal point symbols as defined in your local settings -- substitute your local
regional symbols for these if appropriate.

Rick


Quote:
> Hello All!

> I am new to VB and have a question.  Is there any built in function to
> check if a input contains only number of not?

> Thanks in advance



Sat, 08 Nov 2003 11:09:45 GMT  
 Checking input number
I thank to people responded my quesion.

 Thank you!



Quote:
> Hello All!

> I am new to VB and have a question.  Is there any built in function to
> check if a input contains only number of not?

> Thanks in advance



Sat, 08 Nov 2003 20:10:30 GMT  
 Checking input number
The nice thing is that you can add whatever characters you want to allow
into the "filter" string (in my example, I only allow numeric digits -- and
clearly stated as much).


Quote:
> Watch out. Joe Hart's method will not work with decimal points, + - signs
or
> scientfic notation (for example 4.86e-9)



Sun, 09 Nov 2003 00:27:03 GMT  
 Checking input number
According to your routine:

"1.5" is not numeric and neither is
"2e4" or
"-21"

:-(


Quote:
> Have a look at the IsNumeric( ) function -- but, keep in mind that it
allows
> the following characters as well:

> $  -  +  .  (   )

> Here's one method of checking to see if a string contains ONLY numbers:

> Public Function IsNumbersOnly(ByVal strInput As String) As Boolean
>     Dim i As Integer, j As Integer
>     Const strNumbers As String = "0123456789"

>     For i = 1 To Len(strInput)
>         If InStr(1, strNumbers, Mid$(strInput, i, 1)) = 0 Then Exit
Function
>     Next i

>     IsNumbersOnly = True
> End Function

> NOTE:  This function could be made more generic by taking 2 parameters --
> the "string to check" and the "filter".  That way you could do filtering
on
> any substring (not just numbers).

> Good Luck,

> Joe



> > Hello All!

> > I am new to VB and have a question.  Is there any built in function to
> > check if a input contains only number of not?

> > Thanks in advance



Sun, 21 Dec 2003 00:02:51 GMT  
 Checking input number
And that's exactly why I called the "IsNumbersOnly()" and made the
statement:

"Here's one method of checking to see if a string contains ONLY numbers"



Quote:
> According to your routine:

> "1.5" is not numeric and neither is
> "2e4" or
> "-21"

> :-(



> > Have a look at the IsNumeric( ) function -- but, keep in mind that it
> allows
> > the following characters as well:

> > $  -  +  .  (   )

> > Here's one method of checking to see if a string contains ONLY numbers:

> > Public Function IsNumbersOnly(ByVal strInput As String) As Boolean
> >     Dim i As Integer, j As Integer
> >     Const strNumbers As String = "0123456789"

> >     For i = 1 To Len(strInput)
> >         If InStr(1, strNumbers, Mid$(strInput, i, 1)) = 0 Then Exit
> Function
> >     Next i

> >     IsNumbersOnly = True
> > End Function

> > NOTE:  This function could be made more generic by taking 2
parameters --
> > the "string to check" and the "filter".  That way you could do filtering
> on
> > any substring (not just numbers).

> > Good Luck,

> > Joe



> > > Hello All!

> > > I am new to VB and have a question.  Is there any built in function to
> > > check if a input contains only number of not?

> > > Thanks in advance



Sun, 21 Dec 2003 02:58:26 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. check input field, only numbers accepted

2. Check if input is number or text?

3. How to check if control is a part of an array (without checking err.number)

4. Checking user input in form against table

5. checking input of data

6. Repost: checking user input

7. How to check user input?

8. check user input

9. checking input

10. Advise on the checking of input text into text box

11. Checking For Numeric or Text input?

12. Checking user input in a DBgrid

 

 
Powered by phpBB® Forum Software