Solved

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

Posted on 2003-12-09
17
361 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:josephfluckiger
  • 7
  • 7
  • 2
  • +1
17 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9909097
Dim s as string

if Asc(s) >= 48 and Asc(s) <= 57 then
  msgbox "it is between 0 and 9"
end if
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9909219
you could use a regular expression object to test for your pattern,,,
Will write a little function here and post in a bit....
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 300 total points
ID: 9909241
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Accepted Solution

by:
Dabas earned 300 total points
ID: 9909248
josephfluckiger,
Another way to do it is using Val

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

Dabas
0
 
LVL 5

Assisted Solution

by:mccainz2
mccainz2 earned 200 total points
ID: 9909325
Dim objRXP As New RegExp
    objRXP.Pattern = "^\d[\d]*\d$"
    If objRXP.Test("1444444444") Then
        MsgBox "match"
    End If
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9909331
Definitely check my Pattern , I am EXTREMELY rusty with patterns ... tested on just a few so be wary...
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9909346
BTW:
To use the object simply add a reference to Microsoft VBscript Regualr Expressions
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9909351
Why not plainly "\d+" ?
\d = digit
+ = one or more

Dabas
0
 
LVL 1

Author Comment

by:josephfluckiger
ID: 9909356
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
 
LVL 27

Expert Comment

by:Dabas
ID: 9909361
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
 
LVL 5

Expert Comment

by:mccainz2
ID: 9909379
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
 
LVL 1

Author Comment

by:josephfluckiger
ID: 9909393
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
 
LVL 27

Expert Comment

by:Dabas
ID: 9909413
What about "^\d+$" ?
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9909419
P.S
I only "discovered" regex about a week ago!

Dabas
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9909425
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
 
LVL 5

Expert Comment

by:mccainz2
ID: 9909432
"^\d+$"  looks like that one works!
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9909454
Sometimes I am a fast learner!

Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question