Form Validation ASP Script - ASP Web Pro
  Active Server Pages Programming by ASP Web Pro

ASP Scripts

BASIC FORM VALIDATION

For consistency purposes, it is always a good idea to validate some of your primary form fields. In other words, the user will be required to make an entry in certain fields or the form will not submit.

To get started, create a form page called form.asp and copy the below code into your page:

<form name="YourFormName" method="Post" action="confirm.asp">
<table>
<tr><td>Email: </td>
<td><input type="text" name="Email" size="50"></td></tr>
<tr><td>First Name: </td>
<td><input type="text" name="FirstName" size="50"></td></tr>
<tr><td>Last Name: </td>
<td><input type="text" name="LastName" size="50"></td></tr>
<tr><td>Subject: </td>
<td><input type="text" name="Subject" size="50"></td></tr>
<tr><td>Comments: </td></tr>
<tr><td><textarea cols="50" name="Comments" ></textarea></td></tr>
</table>

<input type="submit" name="Submit" value="Submit">
</form>

In this case, we will make the Email, Subject, and Comments fields all required.

To validate these fields all have entries, create a page called confirm.asp and copy the below code into your page:

<%
DIM strEmail, strSubject, strComments
strEmail = Request.Form("Email")
strSubject = Request.Form("Subject")
strComments = Request.Form("Comments")

IF strEmail <> "" AND strSubject <> "" AND strComments <> "" THEN

' Process the form as you like here
' For example enter form to your database or send it via email

ELSE

Response.Write "<p>Please click back on your browser and complete the following fields:</p>"
IF strEmail <> "" THEN
ELSE
Response.Write "<b>• Email</b><br>"
END IF
IF strSubject<> "" THEN
ELSE
Response.Write "<b>• Subject</b><br>"
END IF
IF strComments <> "" THEN
ELSE
Response.Write "<b>• Comments</b><br>"
END IF

END IF
%>

That's it in a nutshell! Now, you have a standard form with required fields. Once you understand the basics of this, you can get a lot more sophisticated and really customize your validation to meet your spcific needs.

< Back to ASP Scripts


 

 

Main Menu
Home
ASP Scripts
ASP Tutorials
JavaScripts
Awards
Contact Us

Email This Page

Other Resources
ASP Web Hosting
Search Engine Submission
Programming Help

 

 

 

Other ASP Web Sites

Site Map

 

Last Updated:
02 December 2008