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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobbit31Commented:
Dim s as string

if Asc(s) >= 48 and Asc(s) <= 57 then
  msgbox "it is between 0 and 9"
end if
mccainz2Commented:
you could use a regular expression object to test for your pattern,,,
Will write a little function here and post in a bit....
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

DabasCommented:
josephfluckiger,
Another way to do it is using Val

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

Dabas

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

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

Dabas
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!
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
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
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.
DabasCommented:
What about "^\d+$" ?
DabasCommented:
P.S
I only "discovered" regex about a week ago!

Dabas
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 :)
mccainz2Commented:
"^\d+$"  looks like that one works!
DabasCommented:
Sometimes I am a fast learner!

Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.