Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Beginner
May 10, 1999

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 %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<H2>Vartype check</H2>
<% 
quantity = request.form("text1")
Response.Write "The vartype is: " & vartype(quantity) & "<p>"
If IsNumeric(quantity) then
  Response.Write "It resembles a number, so let's do quantity=CDbl(quantity)<P>" 
  quantity= CDbl(quantity)
  Response.Write "The vartype is now: " & vartype(quantity) & "<p>"
else
  Response.Write "Whatever you submitted, it can't be converted to a numeric value."
end if
%>
<P> </P>
<form action="type.asp" method=post>
<input type=text name="text1">
<input type=submit>
</form>
</BODY>
</HTML>
Ken Cox
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date