I loop over string variable data which may have integer numeric value, like "123". If this string variable has numeric value, I want to indicate it and thought to use some like this:
If IsNumeric(CInt(data)) Then
WScript.Echo "Number"
Else
WScript.Echo "String"
End If
But CInt() raises error every time data variable can't be converted to integer:
Type mismatch: 'CInt'
How can I indicate if string has integer value in vbscript?
IsNumeric Function of vb script can be used to determine whether an expression can be evaluated as a number.It returns Boolean value depending upon expression
Please be noted that IsNumeric returns False if expression is a date expression.
Now, in your code you have mistaken that even if it is not number, you trying to convert it into integer
You can use this in your code like this--
If IsNumeric(data) Then
WScript.Echo "Number"
Else
WScript.Echo "String"
End If
only for integers:
If VarType(data) = vbInteger Then
WScript.Echo "Integer"
Else
WScript.Echo "Something else"
End If
for numbers:
If IsNumeric(data)
WScript.Echo "Number"
Else
WScript.Echo "string"
End If
Related
What is the quickest and easiest way (in Classic ASP) to check if a string has some string (that has a length greater than 0) i.e. NOT "Null", "Nothing", "Empty", or '' empty string
To make sure that the Variant you deal with is of sub-type "string", you need the VarType or TypeName function. To rule out zero length strings, you need Len(). To guard against strings of space, you could throw in a Trim().
Code to illustrate/experiment with:
Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Function toLiteral(x)
Select Case VarType(x)
Case vbEmpty
toLiteral = "<Empty>"
Case vbNull
toLiteral = "<Null>"
Case vbObject
toLiteral = "<" & TypeName(x) & " object>"
Case vbString
toLiteral = qq(x)
Case Else
toLiteral = CStr(x)
End Select
End Function
Function isGoodStr(x)
isGoodStr = False
If vbString = VarType(x) Then
If 0 < Len(x) Then
isGoodStr = True
End If
End If
End Function
Dim x
For Each x In Array("ok", "", " ", 1, 1.1, True, Null, Empty, New RegExp)
WScript.Echo toLiteral(x), CStr(isGoodStr(x))
Next
output:
cscript 26107006.vbs
"ok" True
"" False
" " True
1 False
1.1 False
True False
<Null> False
<Empty> False
<IRegExp2 object> False
Here's a one-liner that dodges all the trouble with Null by concatenating the value with an empty string. It works for Null, Empty, "", and, of course, strings with actual length! The only one it doesn't (nor shouldn't) work for is Nothing, because that's for object variables, of which a string is not.
isNullOrEmpty = (Len("" & myString) = 0)
You could try having something like this:
Function nz(valToCheck, valIfNull)
If IsNull(valToCheck) then
nz = valIfNull
Else
nz = valToCheck
End if
End function
and then you would use it like this:
if nz(var,"") <> "" then
'--string has something in it
else
'--string is null or empty
end is
You can use the VarType() function to check if it is a string, then you can check if the string is not empty. This statement will only pass through a string that isn't empty.
If VarType(MyString) = 8 Then
If MyString <> "" Then
'String is Not Null And Not Empty, code goes here
End If
End If
This worked for me:
if mystring = "" then wscript.echo "Empty string"
else wscript.echo "String is not empty"
<%
Dim x,y
x = "abcdefg"
'counting length of string
y = Len(x)
Response.Write (y)
'checking string is empty or not
If Len(x) = 0 then
Response.Write ("<p>String is empty</p>")
Else
Response.Write ("<p>String is not empty</p>")
End If
%>
Hope this is helpful.
I have a web service which returns a json response as follows:
"database" ; True
"cpu usage" ; 30%
"connection response" ; 1
"memory" ; 48%
The requirement is to create a vb script which would read through the results, compare it against a set threshold and set a flag accordingly.
That is, I need the result to say "green" if the value against "database" is "true", cpu usage is less than 80%, connection response is more than 0 and memory usage is less than 80%.
Could someone please help me with the above request. This is actually to be used with SCOM monitoring.
Your JSON will be more like this. Note that I have changed the variable names to remove spaces - you will need to modify the code accordingly if these guesses were wrong. In JSON variable names and any non-numeric values are in quotes. Typically you would use a JSON parser to handle this but if it really is this simple you can use some simple string handling code to proceed.
{
"database": "true",
"cpu_usage": 30,
"connection_response": 1,
"memory": 48
}
Call this function passing it the JSON string that you get from the service. It works on the basis that a JSON string is a string and we can chop it about to get usable values IF it is of simple format. If it becomes a more complex message then you will need to search for a JSON parser for VB, or if the interface can respond in XML you will find it much easier to handle in VB.
This is VB6 code (easier for me to test with) - you will need to remove all of the 'as string', 'as integer' etc from the variable declares for VB Script. I have included a val() function for VBScript sourced from here though not tested with my function. You need val() as the JSON is string formatted and if you try to compare numeric values to strings you will get unexpected results.
'
' Function to return RED or GREEN depending on values in simple JSON
'
Function checkStatus(sJSON As String) As String
Dim aVals() As String, aParams() As String, i As Integer, sName As String, sVal As String
Dim bDatabase As Boolean, bCPU As Boolean, bConnection As Boolean, bMemory As Boolean
aVals = Split(sJSON, ",")
For i = 0 To UBound(aVals)
aVals(i) = Trim(aVals(i)) ' remove any leading & trailing spaces
aVals(i) = Replace(aVals(i), "{", "") ' remove braces open
aVals(i) = Replace(aVals(i), "}", "") ' remove braces close
aVals(i) = Replace(aVals(i), """", "") ' remove quotes > "database: true"
Debug.Print "vals[" & i & "]=" & aVals(i)
If Len(aVals(i)) > 0 Then ' should catch any dodgy JSON formatting but may need refinement
aParams = Split(aVals(i), ":") ' split the line e.g. "database: true" > "database" and " true"
If UBound(aParams) > 0 Then
sName = LCase(Trim(aParams(0))) ' now we have sName = "database"
sVal = LCase(Trim(aParams(1))) ' and sVal = "true"
Select Case sName
Case "database"
bDatabase = False
If sVal = "true" Then
bDatabase = True
End If
Case "cpu_usage"
bCPU = False
If Val(sVal) > 80 Then
bCPU = True
End If
Case "connection_response"
bConnection = False
If Val(sVal) > 0 Then
bConnection = True
End If
Case "memory"
bMemory = False
If Val(sVal) < 80 Then
bMemory = True
End If
End Select
End If
End If
Next i
checkStatus = "RED" ' default return value to indicate an issue
' compare the flags to decide if all is well.
If bDatabase And bCPU Then 'And bConnection And bMemory Then
checkStatus = "GREEN"
End If
End Function
Function Val( myString )
' Val Function for VBScript (aka ParseInt Function in VBScript).
' By Denis St-Pierre.
' Natively VBScript has no function to extract numbers from a string.
' Based shamelessly on MS' Helpfile example on RegExp object.
' CAVEAT: Returns only the *last* match found
' (or, with objRE.Global = False, only the *first* match)
Dim colMatches, objMatch, objRE, strPattern
' Default if no numbers are found
Val = 0
strPattern = "[-+0-9]+" ' Numbers positive and negative; use
' "ˆ[-+0-9]+" to emulate Rexx' Value()
' function, which returns 0 unless the
' string starts with a number or sign.
Set objRE = New RegExp ' Create regular expression object.
objRE.Pattern = strPattern ' Set pattern.
objRE.IgnoreCase = True ' Set case insensitivity.
objRE.Global = True ' Set global applicability:
' True => return last match only,
' False => return first match only.
Set colMatches = objRE.Execute( myString ) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
Val = objMatch.Value
Next
Set objRE= Nothing
End Function
I need a VBScript loop asking to entered an integer between 1 to 10 including, if wrong symbol or number entered then asking again until the desired number is retrieved from user.
This is what I tried:
Option Explicit
Dim Num
Num=inputbox("Please enter integer number between 1 to 10")
'Checking if entered value is numeric
Do while not isnumeric(Num)
Num=inputbox("Please enter integer number between 1 to 10", "INCORRECT SYMBOL")
Loop
Do while (Num<1 or Num>10)
Num=inputbox("Please enter integer number between 1 to 10 ", "Number is NOT IN RANGE")
Loop
Do while not int(Num)
Num=inputbox("Please enter integer number between 1 to 10 ", "Number is NOT INTEGER")
Loop
does not work: when I enter 3 for example I am getting inputbox saying "Number is NOT INTEGER", when entering a letter I receive error message Type mismatch string, error code 800A00D.
You need one Loop. For each (variant) input you need to check:
is it Empty (User pressed Cancel or X) - Abort
is it a blank (zero length or sequence of spaces) string
is it numeric
is it an integer
is it in range - Accept
As in:
Option Explicit
Dim vNum, sNum, nNum
Do
vNum = InputBox("Please enter an integer beween 1 and 10 (inclusive)")
If IsEmpty(vNum) Then
WScript.Echo "Aborted"
Exit Do
Else
sNum = Trim(vNum)
If "" = sNum Then
WScript.Echo "Empty string"
Else
If IsNumeric(sNum) Then
nNum = CDbl(sNum)
If nNum <> Fix(nNum) Then
WScript.Echo "Not an Integer"
Else
If nNum < 1 Or nNum > 10 Then
WScript.Echo "Not in range"
Else
WScript.Echo nNum, "is ok"
Exit Do
End If
End If
Else
WScript.Echo "Not a number"
End If
End If
End If
Loop
WScript.Echo "Done"
Using different variables for the different data types may be pedantic, but should show why you had type problems.
Your
Do while not int(Num)
does not work as expected because here Num is a number between 1 and 10; rounding off the (not existing) fractional part gives Num again; Num evaluated in a boolean context/as a bool gives (always) True.
Update wrt comment:
Trim removes space from the head or tail of a string; WScript.Echo sends output to the console (cscript) or a dialog box (wscript).
Update:
As this question shows, I didn't make clear that pressing Cancel or X (Close) sets vNum to an empty variant, which is different from an empty/zero length string. So it should be treated as indication of the users intention to abort.
BTW: You need to read the Docs, but you can't believe them always (cf. here).
DISCLAIMER: I'm still an uber-n00b with programming concepts and know just enough VBS to hurt myself, so I'm likely to slur/slaughter some of the terms/concepts/ideas that are entailed with this program I'm trying to write. You, the vastly superior programmer who has every right to flame me, have been warned.
I've been trying to write a VBScript to validate data, specifically Dates. Since my users are kind of poor with keyboards, I figured I'd make it easy by separating the entry of each part of the date (Month & Day only, I've got the Year set statically).
Previously, I was having problems with validating the numbers with only 1 "Do...Loop", as I was trying to verify if it was Numeric Input and checking at the same time if it was within the specified range (1 - 12, for the 12 months of the year).
This is what the previous code roughly looked like:
Do
' If...Then Statements Here
Loop Until (dtFirstMonth > 0) _
And (dtFirstMonth < 13) _
And IsNumeric(dtFirstMonth) _
And (dtFirstMonth <> "") _
And (dtFirstMonth <> vbNull)
This often resulted in "Data Type Mismatch" errors, so I had to split the Validation Critera to two separate "Do...Loop" statements, as you can see in the current code I have below:
Sub srTest()
Do
dtFirstMonth = InputBox("Please Enter a Numeric Month for the Starting Range", _
"Starting Range Month")
If (dtFirstMonth = vbNull) _
Or (dtFirstMonth = "") _
Or Not IsNumeric(dtFirstMonth) Then
MsgBox "Please Enter a Valid Numeric Month",, "Enter Month Number"
ElseIf (dtFirstMonth <> vbNull) _
And (dtFirstMonth <> "") _
And IsNumeric(dtFirstMonth) Then
Do
dtFirstMonth = Round(dtFirstMonth)
Wscript.Echo dtFirstMonth ' Infinite Loop Here (Basically, As Soon As We Get Into Loop with a Value of 1, We're Stuck)
dtFirstMonth = CInt(dtFirstMonth)
' Must Convert User Input to Integer to
' Prevent Data Mismatch Errors In
' Following "If" Statement; Besides,
' It Passed the First Test to be a
' Numeric Value in the First Place
If (dtFirstMonth < 1) Or (dtFirstMonth > 12) Then
MsgBox "Please Enter a Valid Numeric Month",, "Enter Month Number"
Exit Do
' Drop Out of 2nd Level Loop to
' Enter Into 1st Level Loop
End If
Loop Until (dtFirstMonth > 0) _
And (dtFirstMonth < 13) _
And IsNumeric(dtFirstMonth) _
And (dtFirstMonth <> "") _
And (dtFirstMonth <> vbNull)
If (dtFirstMonth < 1) Or (dtFirstMonth > 12) Then
dtFirstMonth = ""
End If
' dtFirstMonth Was Converted to Integer Earlier
' This is to Meet the Value that Didn't Pass
' the Nested Do & If Statement (Level 2 Do Loop)
' Sets dtFirstMonth to "Empty String" to Continue
' Looping in the Level 1 "Do...Loop" Statement;
' If Omitted, Level 1 "Do...Loop" is Satisfied,
' Thus Ending the Subroutine (Since the Value
' of dtFirstMonth is Still a Numeric Value)
End If
Loop Until IsNumeric(dtFirstMonth) _
And (dtFirstMonth <> "") _
And (dtFirstMonth <> vbNull)
Wscript.Echo dtFirstMonth
End Sub
srTest
I had to set up the 1st "Do...Loop" to check that the User Input (dtFirstMonth) was a indeed a Numeric Value and not a Null Value nor an Empty String. The Nested "Do...Loop", or 2nd "Do...Loop", statement is where I have the same Criteria plus the extra Criteria defining the desired ranges (any number between 1 and 12).
This is working perfectly for number 2-12, but when the script parses the number 1, I enter into an Infinite Loop.
I've checked to make sure that the Infinite Loop is occurring in the 2nd "Do...Loop" by replacing the entire 2nd "Do...Loop" section with "Wscript.Echo dtFirstMonth". By doing this, I get the expected results: a single Echo, not an infinite number of them (technically, I get 2, as I do have another "Wscript.Echo dtFirstMonth" string at the bottom of the Subroutine for the purpose of debugging, but either way, it's not an Infinite Loop).
I've also changed the criterion for the lower range to be like this, yet this doesn't remediate the error:
Do
' If...Then Statements Here
Loop Until (dtFirstMonth >= 1)
I've also tried this, with no resulting success:
Do
' If...Then Statements Here
Loop Until (dtFirstMonth >= CInt(1))
In all reality, there really is no need for this segment, since I converted the User's Input to an integer anyway.
Since this was starting to get confusing, I decided to add the "Round" statement before the script passed the User's Input to the "CInt" function, hoping that it would make sure that it wasn't getting caught as a 0 value or decimal value somehow; yes, this is irrational thought on my part, but I still wanted to explore all avenues (there's also the fact that I have some users with "Fat Finger Syndrome" and some others with "Abuse The Program" mentality, so I figured I'd make sure the script accounted for decimal entries). I added the "Round" string before and after the nested "Do...Loop" and I still had the Infinite Loop issue.
This is as far as I've been able to get on this and now I'm stuck.
I realize that there are likely better ways to do Date/Time Validation in VBScript, and I'm certainly open to any new suggestions, but I'd love to solve this for the pure sake of edification.
Way too much code for a simple input of a number. Just try to keep it short and simple. Example:
Do
dtm = InputBox("Please Enter a Numeric Month for the Starting Range", _
"Starting Range Month")
Select Case True
Case isNull(dtm), (not isNumeric(dtm)), dtm = "", dtm = empty, (dtm < 1 OR dtm > 12)
' too exhaustive, but just for the sake of the example.
MsgBox "Please enter an amount between 1 and 12"
Case else
' Hey, this seems to be a valid amount!
Exit do
End Select
Loop While True
' Do something with dtm
Just showed you some creative Select Casing, this supports lazy exit, so if a value is Null, it escapes before getting evaluated where evaluating could throw an error.
The problem is with your attempt to check for null values:
(dtFirstMonth = vbNull)
The proper way to check for nulls, as demonstrated in AutomatedChaos' answer, is with the IsNull function. vbNull is actually a constant that's used with the VarType function. The value of vbNull is 1, which is why that particular value behaved differently from other entries. That's the fundamental problem, and if you replace every dtFirstMonth = vbNull with IsNull(dtFirstMonth), you won't get an infinite loop when entering 1.
Now, the actual place where your code infinitely loops is interesting. I would expect the first conditional If (dtFirstMonth = vbNull) to evalute true for an entry of "1", and you would get the message box "Please enter a valid numeric month". However, the Else condition is triggered. This is weird because normally, when you compare a string to a number, VBScript will attempt to convert the number to a string or vice versa, so if dtFirstMonth is "1", it should be equal to vbNull (which is 1). However, there appears to be a special case when you compare a string variable to an integer variable. See this example:
' vbNull = 1, built-in constant
dtFirstMonth = "1"
MsgBox (dtFirstMonth = vbNull) ' False
MsgBox ("1" = vbNull) ' True
MsgBox (dtFirstMonth = 1) ' True
MsgBox (CInt(dtFirstMonth) = vbNull) ' True
I don't know if this is a bug or just an obscure detail about VBScript's implicit conversion, but it does illustrate that implicit conversion can be unpredictable in VBScript.
As far as alternative methods go, you may be interested in the IsDate function, which returns True if the given expression can be converted into a date. It may not be ideal for your users and their keyboard skills, but it would reduce your code to:
Do
str = InputBox("Enter a date (MM/DD)")
If IsDate(str) Then
Exit Do
Else
WScript.Echo "Please enter a valid date"
End If
Loop
dt = CDate(str)
' Put your static year here; CDate will default to the current year
dtActual = DateSerial(2007, Month(dt), Day(dt))
WScript.Echo (dtActual) & " - Thanks!"
Note that IsDate should return False for the typical edge cases ("", Null, Empty, etc.), so there's no need for a separate check.
I found a simple program to generate Date and time from http://rindovincent.blogspot.com/p/vbscript-programs.html. I am pasting the same program with permission.
<html>
<body>
<center>
<script type="text/vbscript">
d=CDate("October 22, 2010")
document.write(d&"<br/>")
document.write("Current system date is:"&date&"<br/>")
document.write("Current system time is:"&time&"<br/>")
document.write(DatePart("m",Now())&"<br/>")
document.write(DateAdd("yyyy",1,"31-Jan-10") & "<br />")
document.write(MonthName(10,true)& "<br />")
fromDate="22-sep-10 00:00:00"
toDate="21-oct-10 23:59:00"
document.write(DateDiff("m",fromDate,toDate)&"<br />")
document.write(DateDiff("y",fromDate,toDate) & "<br />")
document.write(DateDiff("w",fromDate,toDate) & "<br />")
document.write(DateDiff("h",fromDate,toDate) & "<br />")
</script>
</center>
</body>
</html>
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
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