Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2003-12-09
Medium Priority
365 Views
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
Question by:josephfluckiger
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 7
• 7
• 2
• +1

LVL 18

Expert Comment

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

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

Dabas earned 1200 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

Dabas
0

LVL 27

Accepted Solution

Dabas earned 1200 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

mccainz2 earned 800 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

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

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

LVL 27

Expert Comment

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

Dabas
0

LVL 1

Author Comment

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.

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

0

LVL 27

Expert Comment

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

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

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

ID: 9909413
0

LVL 27

Expert Comment

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

Dabas
0

LVL 5

Expert Comment

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

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

LVL 27

Expert Comment

ID: 9909454
Sometimes I am a fast learner!

Thanks!
0

## Featured Post

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most pâ€¦
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). Uâ€¦
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process fromâ€¦
###### Suggested Courses
Course of the Month8 days, left to enroll