Validate Numbers With IsNumeric() and Vartype() in VBScript

When accepting input from an HTML form, you can’t assume that the user will provide the right type of data–you have to validate. For instance, if your application expects users to type a number, but they type something that can’t be interpreted as a number, your code could crash. Use the IsNumeric() function to determine whether a value can be safely converted into a number. Another useful function is Vartype() which tells you whether the variable you are using will handle the size of number that you intend to put into it. This code shows both functions in an Active Server Pages script:

 <%@ Language=VBScript %>

Vartype check

<% quantity = request.form("text1")Response.Write "The vartype is: " & vartype(quantity) & "

"If IsNumeric(quantity) then Response.Write "It resembles a number, so let's do quantity=CDbl(quantity)

" quantity= CDbl(quantity) Response.Write "The vartype is now: " & vartype(quantity) & "

"else Response.Write "Whatever you submitted, it can't be converted to a numeric value."end if%>


Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: