21

Within a VBScript, I need to make sure the user inputs a integer.

Here is what I have now :

WScript.Echo "Enter an integer number : "
Number = WScript.StdIn.ReadLine
If IsNumeric(Number) Then
    ' Here, it still could be an integer or a floating point number
    If CLng(Number) Then
       WScript.Echo "Integer"
    Else
       WScript.Echo "Not an integer"
    End If
End if

The problem is that CLng() doesn't test if my number is an integer : the number is converted anyway.

Is there a way to check if a number is an integer ?

EDIT :

The suggested answer doesn't work as well for me. Here is a new version of my code :

WScript.Echo "Enter an integer number : "
Number = WScript.StdIn.ReadLine
If IsNumeric(Number) Then
   ' Here, it still could be an integer or a floating point number
   If Number = CLng(Number) Then
      WScript.Echo "Integer"
   Else
      WScript.Echo "Not an integer"
   End If
End if

and here is the output :

U:\>cscript //nologo test.vbs
Enter an integer number :
12
Not an integer

U:\>cscript //nologo test.vbs
Enter an integer number :
3.45
Not an integer
Jérôme
  • 26,567
  • 29
  • 98
  • 120

9 Answers9

19

This will actually work:

WScript.Echo "Enter an integer number : "
Number = WScript.StdIn.ReadLine
If IsNumeric(Number) Then
    ' Here, it still could be an integer or a floating point number
    If CStr(CLng(Number)) = Number Then
       WScript.Echo "Integer"
    Else
       WScript.Echo "Not an integer"
    End If
End If

Previously, the problem was that you were comparing a string vs an integer which would never evaluate to true.

Now, you take a string, check if it's numeric, transform it to CLng() which will return only the integer part of the number, transform it back to a string and finally compare it against the original string.

If you enter... "asdasD" (or any other non-numeric thing) it doesn't pass the "isNumeric" check.

If you enter "10.5" (as a string) when converted to CLng() you get 10 when then gets converted to "10" and compared against "10.5". Since the strings don't match, it says it's not an integer.

If you enter "10" converted to CLng() it's 10, back to string it's "10" which returns a true when matching it against "10", meaning it is an integer.

A few years too late I know, but I was looking into this myself just now and got puzzled by it. Hope it helps anyone else wondering around like myself.

cogumel0
  • 191
  • 1
  • 2
  • the difference between your answer and backslash17 is this line : `If CStr(CLng(Number)) = Number Then`. Is the explicit conversion with CStr really needed ? I have the feeling that VBS is doing the implicit conversion for us ! – Jérôme Jan 11 '12 at 10:58
  • 1
    Yes, the explicit conversion is needed. Note this: "Previously, the problem was that you were comparing a string vs an integer which would never evaluate to true." cogumel0, thank you for figuring this out! – Eugen Labun Mar 26 '12 at 11:44
  • This was the only example on this page that worked for me (the accepted answer didn't). The `CStr()` conversion seems critical. – gbmhunter Nov 12 '14 at 04:33
  • Bizarre. I wrapped this code in a quick function: is_integer. is_integer(123) returns FALSE; but is_integer("123") returns TRUE. Causing all kinds of bugs because unquoted numbers are (ironically) not seen as numbers... or something – Stephen R Dec 22 '16 at 00:36
  • Yes, this answer assumes the value you want to check is a string; see [this other answer](https://stackoverflow.com/a/41288474/587365) if you want it to work regardless whether the passed-in argument is a string or a number – Andrew Spencer Jun 25 '19 at 09:26
  • `1e10` is `IsNumeric` yet `CLng` will fail with an overflow :( There is no decent built in function for such a basic validation. – Salman A Jan 27 '21 at 09:14
13

This is very similar to your code:

WScript.Echo "Enter an integer number : "
Number = WScript.StdIn.ReadLine
If IsNumeric(Number) Then
    ' Here, it still could be an integer or a floating point number
    If CLng(Number) = Number Then
       WScript.Echo "Integer"
    Else
       WScript.Echo "Not an integer"
    End If
End If
backslash17
  • 5,300
  • 4
  • 31
  • 46
  • IsNumeric has a problem. Enter "12,6,14" and IsNumeric returns true, and will throw an exception at the CLng statement. – LarryBud Jan 16 '16 at 22:58
  • 2
    `CLng(Number) = Number` will not work as the operands have a different type thus the check will fail, see answer of @cogumel0. – ViRuSTriNiTy Aug 31 '16 at 16:35
4

cogumel's answer above almost gets it, but failed for me in odd ways. I discovered that it would return true for "5" (in quotes), but not 5 (without quotes). When doing the final comparison you need to convert the original input to string as well, to make it all work reliably. Here it is wrapped in a neat function:

public function is_integer( input )
    is_integer = false
    If IsNumeric(input) Then
        If CStr(CLng(input)) = CStr(input) Then is_integer = true
    End If
end function

I've also tested this with zero (true), negative integers (true), both in and out of quote marks.

Stephen R
  • 3,512
  • 1
  • 28
  • 45
3

If you do something like this, it should work:

if Number = CInt(Number) Then

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
2

CLng would throw an exceprion for numbers bigger than 2147483647 or lower than -2147483648.

WScript.Echo "Enter an integer number : "
Number = WScript.StdIn.ReadLine
If IsNumeric(Number) Then
    ' Here, it still could be floating point number out of CLng's range
    If CDbl(Number) <= 2147483647 and CDbl(Number) >= -2147483648 Then
    ' Here, it still could be floating point number
        If CLng(Number) & "" = Number & "" Then
            WScript.Echo "Integer"
        Else
            WScript.Echo "Not an integer"
        End If
    Else
        WScript.Echo "Not an integer"
    End If
End If
1

Here is a slightly different way to do it, it doesn’t matter if you are passing an integer, long, or a string. The function also checks if the number is a positive number, but you can change that by removing Abs().

If IsNumber("1010") Then
 MsgBox "Valid whole number"
Else
 MsgBox "Not a valid whole number"
End If

Function IsNumber(ByVal Number)

 IsNumber = False
 If IsNumeric(Number) Then
  If CStr(Number) = CStr(Abs(Fix(Number))) Then
   IsNumber = True
  End If
 End If

End Function
Safwan
  • 181
  • 5
-1

I found this simple program to validate numeric value from http://rindovincent.blogspot.com/p/vbscript-programs.html and with permission i am pasting the same. I hope it will be helpful for beginners like me

<HTML>
<HEAD><TITLE>Simple Validation</TITLE>
<SCRIPT LANGUAGE="VBScript"> 
<!--
Sub Submit_OnClick
  Dim TheForm
  Set TheForm = Document.ValidForm
  If IsNumeric(TheForm.Text1.Value) Then
    If TheForm.Text1.Value < 18 Or TheForm.Text1.Value > 40 Then
      MsgBox "Age must be above 18"
    Else
      MsgBox "Thank You"
    End If
  Else
    MsgBox "Please enter a numeric value"
  End If
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H3>Simple Validation</H3><HR>
<FORM NAME="ValidForm">
Enter your age: 
<INPUT NAME="Text1" TYPE="TEXT" SIZE="2">
<INPUT NAME="Submit" TYPE="BUTTON" VALUE="Submit">
</FORM>
</BODY>
</HTML> 
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
Gautham
  • 27
  • 1
  • Client-side scripts are not a good answer. Never trust user input; check it server-side after submission – Stephen R Sep 03 '18 at 00:45
-1
On Error Resume Next
Test = WScript.Arguments(0)

NotFloat = False
IsNumber = False

If IsNumeric(Test) Then IsNumber = True

If CStr(CLng(Test)) = CStr(Test) Then NotFloat = True

If IsNumber And NotFloat And Test <> "" Then
    WScript.Echo "It's an integer :)"
Else
    WScript.Echo "Not an integer :("
End If

This is basically the same as what some people already had - I made it super easy for just about anyone to follow, and also added a test for "" because sending nothing as an argument still passed as an integer.

Rob
  • 1
  • 1
-4

another way,

if number > 0 then
...
end if
ghostdog74
  • 327,991
  • 56
  • 259
  • 343