|
Validate numerical data
OK you have a form where a user enters a number
representing their age but how can you ensure that the number
is valid and falls in a certain range , here is an answer which
makes sure the data is numeric and the data also falls into
the range of 1 and 120 . If you are over 120 years old I apologise
.
Here is the codet:
<form method="post"
action ="<%= Request.ServerVariables("SCRIPT_NAME")
%>" >
<input type ="text" name="age"><br>
<input type="submit">
</form>
<%
Dim strAge , intAge , blnValid
If Request.ServerVariables("CONTENT_LENGTH") <>
0 Then
strAge = Trim(Request.form("age"))
'if strAge is a number then we go on
If isNumeric(strAge) Then
'convert strAGe to an integer
intAge = CInt(strAge)
' if age is between 1 and 120 then success
If 1<= intAge and 120 >= intAge Then
blnValid = True
End If
'if a number is entered then display this
Else
Response.Write "You didnt enter a valid number<br>"
End If
'if you have a valid number then display a message
If blnValid = True Then
Response.Write("Your age is " & intAge)
'if number is invalid then display a different message
Else
Response.Write("Enter an age between 1 and 120 ")
End If
End If
%>
|