开发者

VBScript : checking if the user input is an integer

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 int开发者_如何转开发eger : 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


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.


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


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.


If you do something like this, it should work:

if Number = CInt(Number) Then


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


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


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> 


another way,

if number > 0 then
...
end if
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜