Server Time:
Thursday May 22 2008 02:03 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Implementing FORM Error Checking On Your Pages!
by: Pablo Varando
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

How can I perform error checking on a form to ensure that the user is entering data into the fields?

You have two ways to do this.

The first is to use "<CFFORM>" not recommended, but it works ;) It's not recommended because it doesn't give you the total control you truly need to have, but it does work!

<cfform action="apage.cfm" method="post">
    <table border="0" width="100%" cellspacing="1">
        <tr>
            <td width=
"100%" bgcolor="#99CC99"><b><i><font size="3" face="Verdana">Contact My Company</font></i></b></td>
        </tr>
        <tr>
            <td width=
"100%" bgcolor="#EFEFEF"><font face="Verdana" size="1">A form explanation goes here</font></td>
        </tr>
        <tr>
            <td width=
"100%">
            <table border=
"0" width="100%" cellspacing="1" cellpadding="2">
                <tr>
                    <td width=
"50%"><font face="Verdana" size="1"><strong>Your Name:</strong></font></td>
                    <td width=
"50%"><cfinput type="text" name="fullname" size="40" required="Yes" message="Please enter your full name!"></td>
                </tr>
                <tr>
                    <td width=
"100%" colspan="2" bgcolor="#EFEFEF"></td>
                </tr>
                <tr>
                    <td width=
"50%"><font face="Verdana" size="1"><strong>Your Email:</strong></font></td>
                    <td width=
"50%"><cfinput type="text" name="email" size="40" required="Yes" message="Please enter your email address!"></td>
                </tr>
                <tr>
                    <td width=
"100%" colspan="2" bgcolor="#EFEFEF"></td>
                </tr>
                <tr>
                    <td width=
"50%"></td>
                    <td width=
"50%"><font face="Verdana" size="1"><input type="submit" value="Send Form" name="Process"></font></td>
                </tr>
                <tr>
                    <td width=
"100%" colspan="2" bgcolor="#EFEFEF"></td>
                </tr>
            </table>
            </td>
        </tr>
    </table>

</cfform>

The second alternative you have is as follows:

<!--- This javascript will check for form errors --->
<SCRIPT LANGUAGE="JavaScript">
    function checkForm(){
                 theform=document.contact

                     // Check that the form has a full name specified in the field called "fullname"
                 if (theform.fullname.value==""){ 
                     alert("Please provide your full name.")
                     theform.fullname.focus()
                     return false;
                }

                    // Check that the form has an email specified in the field called "email"
                if (theform.email.value==""){ 
                    alert("Please provide your email address.")
                    theform.email.focus()
                    return false;
                }

                return true;
                }
</SCRIPT>

<form method="POST" action="apage.cfm" name="contact" onsubmit='return checkForm();'>
<table border="0" width="100%" cellspacing="1">
    <tr>
        <td width=
"100%" bgcolor="#99CC99"><b><i><font size="3" face="Verdana">Contact My Company</font></i></b></td>
    </tr>
    <tr>
        <td width=
"100%" bgcolor="#EFEFEF"><font face="Verdana" size="1">A form explanation goes here</font></td>
    </tr>
    <tr>
        <td width=
"100%">
        <table border=
"0" width="100%" cellspacing="1" cellpadding="2">
            <tr>
                <td width=
"50%"><font size="1" face="Verdana"><strong>Your Name:</strong></font></td>
                <td width=
"50%"><input type="text" name="fullname" size="40"></td>
            </tr>
            <tr>
                <td width=
"100%" colspan="2" bgcolor="#EFEFEF"></td>
            </tr>
            <tr>
                <td width=
"50%"><font size="1" face="Verdana"><strong>Your Email:</strong></font></td>
                <td width=
"50%"><input type="text" name="email" size="40"></td>
            </tr>
            <tr>
                <td width=
"100%" colspan="2" bgcolor="#EFEFEF"></td>
            </tr>
            <tr>
                    <td width=
"50%"></td>
                    <td width=
"50%"><font size="1" face="Verdana"><input type="submit" value="Send Form" name="Process"></font></td>
            </tr>
            <tr>
                    <td width=
"100%" colspan="2" bgcolor="#EFEFEF"></td>
            </tr>
        </table>
        </td>
    </tr>
</table>

</form>

Ok, notice the color combinations. The text you have in this color is the name of the form, this is used so the JavaScript can tell which form to validate against [in case you have multiple forms on a page]. The next is the fieldnames fullname and email these are the fields you want verified. The last item is the value "onsubmit='return checkForm();'" in your <form> tag. This will simply tell the page, that when a user clicks the submit button, to execute the JavaScript (which then checks for those fields).

Note that the JavaScript checks and if those fields are EMPTY, it will post an alert (a pop-up message) letting the user know what they forgot to enter, then it will automatically take the cursor to that field, so that the user can actually enter a value.

Questions? Comments? Email Me...


Date added: Wed. October 16, 2002
Posted by: Pablo Varando | Views: 19963 | Tested Platforms: CF5 | Difficulty: Intermediate
Categories Listed: Error Handling Forms

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Delete files and folders in a specified path!
This tutorial will demonstrate how you can delete all files and sub-folders in a specified folder using ColdFusion and Windows! - Date added: Wed. September 7, 2005
Dynamic Last Date Modified?
This tutorial will demonstrate how to display the date a web page was last modified to your visitors dynamically. - Date added: Mon. April 12, 2004
Correct Content (document) serving!
This tutorial will demonstrate how to correctly serve documents via ColdFusion and allow you to correctly name the download as you see fit! - Date added: Tue. February 10, 2004
Creating your very own RSS XML Feeds with ColdFusion MX!
Have you ever wanted to create your very own RSS XML News Feeds? This tutorial will show you how to create an RSS feed that will allow you to syndicate your web site and allow the world to easily use your data! - Date added: Thu. January 15, 2004
Processing XML/RSS feeds with ColdFusion MX
This tutorial will show you how to parse XML files (RSS Feeds) with ColdFusion MX and it uses an EasyCFM.COM Feed for example [Feed: 5 Most Viewed Tutorials]. It shows you how to call it via CFHTTP all the way to parse and display your records! - Date added: Sat. December 27, 2003

Additional Tutorials:
· Changing the form submission page on the fly!

· What is the ID for the record I just inserted?

· Creating a file content crawler with ColdFusion....

· Delete Records From Your Database With ColdFusion!

· Do you want to remember your members?

· Get A Folder Size Using ColdFusion and FSO...

· Preventing People From Leeching Your Images!

· Combining two queries into one..

· CaSe SensitiVe password logins!

· Creating an ODBC Connection within ColdFusion MX Server...

· Print your web pages on the fly!

· Using <CFPOP> and creating an email client for POP3 Email Reading!

· Using CFRegistry to Add Your IP To CF Debug IP List!

· Reading your IIS Log Files with ColdFusion!

· Automatically Adding Smiles To Your Messages!

· Using Arrays in ColdFusion To Properly Display Data....

· Inserting FORM data into multiple database tables!

· Creating, Altering and Deleting database tables with ColdFusion.

· Sending multiple attachments with CFMAIL!

· ColdFusion and .INI Files!

· Clearing your session variables!

· Using PayPal's IPN with ColdFusion!

· Alternating Row Colors!

· Previous / Next n Records

· Using Query String Values....

· A quick intro into the world of Custom Tags!

· A brief demonstration of Fusebox 2.0

· Creating a Newsletter System....

· Count Active Users On Your Site.

· User Defined Functions....

· Creating a user athentication (Login) area.

· DSNLess Coldfusion?

· A Simple Contact Us Page….

· Having Your Database Do The Work… not ColdFusion!

· Retrieving Records From a Database..

· Inserting data into a database
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
JavaScript dependancy
The only thing I would add is a warning to never substitute client side validation for server side validation. Client side validation should only be used for enhancing the user experience. Server side validation is required for data integrity (and security).
Posted by: Steve Sommers
Posted on: 05/25/2004 09:17 PM
CFFORM
If you create your form using cfform, you'll already have javascript validation.. These tutorials should include notes that they are written for older versions of ColdFusion. Read the LiveDocs!
Posted by: Coder
Posted on: 03/31/2006 05:00 AM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
Daily Razor - ColdFusion Hosting

You are 1 of 516 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved (Server: www0001)
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb