Tuesday, 29 November 2011

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:

Email:
First Name:
Last Name:
Subject:
Comments:



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 "

Please click back on your browser and complete the following fields:

"
IF strEmail <> "" THEN
ELSE
Response.Write "• Email
"
END IF
IF strSubject<> "" THEN
ELSE
Response.Write "• Subject
"
END IF
IF strComments <> "" THEN
ELSE
Response.Write "• Comments
"
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.

No comments:

Post a Comment