Easy Error Management - First Trick: Checking for Truly Numeric Values
(Page 2 of 4 )
VBScript has many built-in functions to verify data types, such as IsEmpty(), IsDate(), and so forth. One issue you may encounter in your ASP application involves the IsNumeric() function. This is because VBScript evaluates Hex numbers, numbers in scientific notation, numbers with double notation, and currency all as numeric. This could be a problem because you have coded expecting only whole integers, no d’s, no e’s, and definitely no $’s. So we are therefore called upon to be a little more specific, and this can be accomplished with the following function:
'===================================================
Function isTrulyNumeric( intToCheck )
'===================================================
intToCheck = Trim( intToCheck )
If Len( intToCheck ) = 0 OR IsEmpty( intToCheck ) Then
isTrulyNumeric = False
Else
Dim c, i
isTrulyNumeric = True
For i = 1 To Len( intToCheck )
c = Asc( Mid( intToCheck, i, 1 ) )
If c <= 44 OR c > 57 OR c = 47 Then
isTrulyNumeric = False
Exit For
End If
Next
End If
End Function
Explanation: This function iterates through each character in the given parameter
( intToCheck ), checking them against their ASCII value. The only permitted characters are 0-9, ‘.’, and ‘-‘ (for negatives).
Using this function and VBScript’s other functions, along with some careful planning and analyzing of where you need which data types, will save you endless frustration and the headaches that generally accompany it!
Next: Second Trick: User-Friendly Error Reporting >>
More ASP Articles
More By Justin Cook