Simple!

1. Senior Member
Join Date
Dec 1969
Posts
115

## Simple!

Whats the code to check if a string contains only numbers or is alfabetic?

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## IsNumeric( str )

But note that IsNumeric means that strings such as "-7.235" and "+1.7E-22" are legal, since they *are* numbers, so far as VBS is concerned.<BR><BR>If you want *only* an integer number, you could do this:<BR><BR>&#060;%<BR>Function isInteger( str )<BR> If Not isNumeric( str ) Then<BR> isInteger = False<BR> Else <BR> test1 = CDbl(str) &#039; if it is numeric, then it can be made Double<BR> test2 = Abs( Int(test1) ) &#039; make it a positive integer<BR> isInteger = ( test1 = test2 ) &#039; true only if original was pos int<BR> End If<BR>End Function<BR>%&#062;<BR><BR>That&#039;s not perfect. It will still accept integer numbers that are larger than the biggest possible LONG number. If you want it checked closer than that, it&#039;s not too hard.<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Or maybe this way?

&#060;%<BR>Function isInteger( str )<BR> Dim temp<BR> temp = "BAD"<BR> On Error Resume Next<BR> temp = CLong( str )<BR> On Error GoTo 0<BR> isInteger = ( temp != "BAD" )<BR>End Function<BR>%&#062;<BR><BR>How it works: <BR><BR>If the string can&#039;t be converted to a long integer, then an error is raised. And, in the process, the value of the "temp" variable is *NOT* changed, at all. So we simply ignore the error (via the ON ERROR stuff) and then check, afterwards, to see if the value of temp was, indeed, changed. If it was, the string is a valid long integer.<BR><BR>If you wanted to only accept positive integers, that&#039;s easy to add:<BR><BR>&#060;%<BR>Function isInteger( str )<BR> Dim temp<BR> temp = "BAD"<BR> On Error Resume Next<BR> temp = CLong( str )<BR> If temp &#060; 0 Then temp = "BAD"<BR> On Error GoTo 0<BR> isInteger = ( temp != "BAD" )<BR>End Function<BR>%&#062;<BR>

4. Senior Member
Join Date
Dec 1969
Posts
464

## RE: Or maybe this way?

Thats a good function. Probably better then isNumeric. I think I had problems with vbscript variants. Just because the isNumeric and comparison functions seem to act wierd sometimes. Depending on the variable if its null or whatever. Ever had to do this before? ...<BR><BR>if Var = "" OR isNull(var) then<BR><BR>end if

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## An easier way...

...an more reliable in some circumstances:<BR><BR>If ("" & var) = "" Then<BR> ...<BR><BR>That works no matter whether var is null, empty (which is *not* the same as null!), or a blank string. Because the string concatenation operator is tolerant of tacking on nulls and empties to any string, including "".<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
464

## Nice....

Its so obvious yet I never thought of it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•