Form validation with asp

The example below provides basic query string based form validation

Simple form:

<form name="form1" method="post" action="form.asp?pa=1">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>

Retrieving the form variables:

<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>

Query string to include in form.asp:

<%
'Error Message Query String
ErrMsg=Request.QueryString("em")
'Page Function identifier
PageAction=Request.QueryString("pa")
%>

Add form error messages:

<%
'Example error Messages
If ErrMsg = "bfn" then
ErrorMessage="Blank full name detected"
Elseif ErrMsg = "bl" then
ErrorMessage="Blank location detected"
Elseif ErrMsg = "mfn" then
ErrorMessage="Maximum Characters exceeded in Full Name"
Elseif ErrMsg = "ml" then
ErrorMessage="Maximum Characters exceeded in location"
Elseif ErrMsg = "ufn" then
ErrorMessage="Unidentified Characters in Full Name"
Elseif ErrMsg = "ul" then
ErrorMessage="Unidentified Characters in location"
End If
%>


Add Page function if receiving the form data in same page - prevents function loop

<%
'Select form variables and check values if form is submitted
If PageAction = "1" then
%>


Form error handling:

<%
'character length check - 0 value
If Len(form_full_name)= 0 then Response.Redirect("form.asp?em=bfn") End If
If Len(form_location)= 0 then Response.Redirect("form.asp?em=bl") End If

'character length maximum of 15 characters
If Len(form_full_name)> 15 then Response.Redirect("form.asp?em=mfn") End If
If Len(form_location)> 15 then Response.Redirect("form.asp?em=ml") End If

'Character check in form values
If Instr(form_full_name,"%") > 0 then Response.Redirect("form.asp?em=ufn") End If 'Checks if a percentage character was inputted
If Instr(form_location,"+") > 0 then Response.Redirect("form.asp?em=ul") End If 'Checks if a plus character was inputted
%>


<%
'End If PageAction = "1" then
End If
%>

Code all together: form.asp

<%
'Error Message Query String
ErrMsg=Request.QueryString("em")
'Page Function identifier
PageAction=Request.QueryString("pa")
%>

<%
'Example error Messages
If ErrMsg = "bfn" then
ErrorMessage="Blank full name detected"
Elseif ErrMsg = "bl" then
ErrorMessage="Blank location detected"
Elseif ErrMsg = "mfn" then
ErrorMessage="Maximum Characters exceeded in Full Name"
Elseif ErrMsg = "ml" then
ErrorMessage="Maximum Characters exceeded in location"
Elseif ErrMsg = "ufn" then
ErrorMessage="Unidentified Characters in Full Name"
Elseif ErrMsg = "ul" then
ErrorMessage="Unidentified Characters in location"
End If
%>

<html>
<body>

<%
'Display Error Message
Response.Write(ErrorMessage)
%>
<br>
<form name="form1" method="post" action="form.asp?pa=1">
Full Name: <input type="text" name="txt_full_name">
Location: <input type="text" name="txt_location">
<input type="submit" value="Submit Form">
</form>
</body>
</html>

<%
'Select form variables and check values if form is submitted
If PageAction = "1" then
%>

<%
'Form variables for form1
form_full_name=Request.Form("txt_full_name")
form_location=Request.Form("txt_location")
%>

<%
'character length check - 0 value
If Len(form_full_name)= 0 then Response.Redirect("form.asp?em=bfn") End If
If Len(form_location)= 0 then Response.Redirect("form.asp?em=bl") End If

'character length maximum of 15 characters
If Len(form_full_name)> 15 then Response.Redirect("form.asp?em=mfn") End If
If Len(form_location)> 15 then Response.Redirect("form.asp?em=ml") End If

'Character check in form values
If Instr(form_full_name,"%") > 0 then Response.Redirect("form.asp?em=ufn") End If 'Checks if a percentage character was inputted
If Instr(form_location,"+") > 0 then Response.Redirect("form.asp?em=ul") End If 'Checks if a plus character was inputted
%>



<%
'End If PageAction = "1" then
End If
%>



<%
'Display form values
Response.write(form_full_name)
Response.write(form_location)
%>

0 comments:

Post a Comment

 
Copyright 2009 <?> Why ASP </?>
BloggerTheme by Infinityskins