best way to determine if a string contains only the digits 0- 9

What is the best way to determine if a string contains only the digits 0 to 9.

IsNumeric() doesn't seem to be sufficent, and I'd like to avoid looping through each character, but if that's the only way I guess I'll have to .

(would be easy in perl, but that doesn't help me either ;)

joe
LVL 1
josephfluckigerAsked:
Who is Participating?
 
DabasCommented:
josephfluckiger,
Another way to do it is using Val

If trim(Val(sText)) = Trim(sText) then...

Dabas
0
 
bobbit31Commented:
Dim s as string

if Asc(s) >= 48 and Asc(s) <= 57 then
  msgbox "it is between 0 and 9"
end if
0
 
mccainz2Commented:
you could use a regular expression object to test for your pattern,,,
Will write a little function here and post in a bit....
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DabasCommented:
Hi josephfluckiger,
Agree with mccainz2.
Regular expressions suits this very well, specially since you have perl experience

See http://oldlook.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20528014.html
for a link to MS site where you can download regex library

Dabas
0
 
mccainz2Commented:
Dim objRXP As New RegExp
    objRXP.Pattern = "^\d[\d]*\d$"
    If objRXP.Test("1444444444") Then
        MsgBox "match"
    End If
0
 
mccainz2Commented:
Definitely check my Pattern , I am EXTREMELY rusty with patterns ... tested on just a few so be wary...
0
 
mccainz2Commented:
BTW:
To use the object simply add a reference to Microsoft VBscript Regualr Expressions
0
 
DabasCommented:
Why not plainly "\d+" ?
\d = digit
+ = one or more

Dabas
0
 
josephfluckigerAuthor Commented:
mccainz2, you mean I can have my cake and eat it too! that's fantastic, thanks so much for showing me how to do regx in VB.

Daba's answer does the trick.
CInt(Val(sText))
ended up doing this trick, since I don't even want 0.3, for example, to be a valid entry.

thanks so much all, all your responses were VERY helpful!
0
 
DabasCommented:
mccainz2,
> add a reference to Microsoft VBscript Regualr Expressions
So that is where they are!

I was looking for Microsoft Regular Expressions, and could not find it!

Dabas
0
 
mccainz2Commented:
Why not plainly "\d+" ?

That pattern will match any string as long as it has one or more numbers in it ... example "sssssss1a" will return as a match
0
 
josephfluckigerAuthor Commented:
yea, I've worked with Perl reg expressions for three years, and VB for one. I've always wanted reg expressions in VB, this great guys, thanks.
0
 
DabasCommented:
What about "^\d+$" ?
0
 
DabasCommented:
P.S
I only "discovered" regex about a week ago!

Dabas
0
 
mccainz2Commented:
I know what you mean!
I havent used in em about a year and its practically like I never used them before!!!
I'm sure theres a better expression for the task but at this time of night it's beyond me :)
0
 
mccainz2Commented:
"^\d+$"  looks like that one works!
0
 
DabasCommented:
Sometimes I am a fast learner!

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.