Basic Form Validation

It is common practice to make most of your form fields required so that the user must enter at least some type of data.  One way to do this is with ASP code.

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">
Email: <input type="text" name="Email" size="50"><br>
First Name: <input type="text" name="FirstName" size="50"><br>
Last Name: <input type="text" name="LastName" size="50"><br>
Subject: <input type="text" name="Subject" size="50"><br>
Comments: <textarea cols="50" name="Comments" ></textarea><br>
<input type="submit" name="Submit" value="Submit">

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

To validate these fields all have entries and are not blank, create a form confirmation 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
' The form fields contain data.
' Send the form data to your database or send it via email.

' One or more of the form fields is blank
Response.Write "<p>Please click back on your browser and complete the following fields:</p>"
     IF strEmail <> "" THEN
     Response.Write "<b>• Email</b><br>"
     END IF
     IF strSubject<> "" THEN
     Response.Write "<b>• Subject</b><br>"
     END IF
     IF strComments <> "" THEN
     Response.Write "<b>• Comments</b><br>"
     END IF


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

< Back to ASP Scripts