Validate each form field by:
You can validate input on either the client by adding JavaScript to the form page or the server using HTML and CFML. When you validate on the client, form fields are validated as you fill them in. When you validate on the server, form fields are validated after the form is submitted.
During this chapter, you will perform ColdFusion server-side form field validation using the hidden HTML input form control.
<INPUT TYPE="HIDDEN" NAME="FormFieldName_ColdFusionValidationRule" VALUE="Message">
ColdFusion provides a variety of validation rules, for example:
![]() |
Note: | It's good programming practices to group all hidden form controls at the top of the page. |
This code validates that a user entered data in the LastName form field:
<INPUT TYPE="HIDDEN" NAME="LastName_Required" VALUE="You must enter a last name!">
This code validates that a user entered a numeric value in the Salary form field:
<INPUT TYPE="HIDDEN" NAME="Salary_Float" VALUE="Not a valid salary">
This code validates that a user entered a date value in the StartDate form field:
<INPUT TYPE="HIDDEN" NAME="StartDate_Date" VALUE="Not a valid date">
![]() |
To validate form data: |
SearchForm.cfm
in HomeSite.InsertForm.cfm
.InsertAction.cfm
.You will create InsertAction.cfm
later in this procedure.
<H4>Employee Add Form</H4>
<CFQUERY NAME="GetDepartments" DATASOURCE="HRExpress"> SELECT Department_Name, Department_ID FROM Departments </CFQUERY>
<INPUT TYPE="HIDDEN" NAME="FirstName_Required" VALUE="First Name is Required!">
<INPUT TYPE="HIDDEN" NAME="LastName_Required" VALUE="Last Name is Required!">
<INPUT TYPE="HIDDEN" NAME="StartDate_Required" VALUE="You must enter a date in the proper format (mm/dd/yy).">
<INPUT TYPE="HIDDEN" NAME="StartDate_Date" VALUE="You must enter a date in the proper format (mm/dd/yy).">
<INPUT TYPE="HIDDEN" NAME="Salary_Required" VALUE="You must enter a salary in the proper format (75000).">
<INPUT TYPE="HIDDEN" NAME="Salary_Float" VALUE="You must enter a salary in the proper format (75000).">
<B>EMPLOYEE FIRST NAME</B><BR> <INPUT TYPE="TEXT" NAME="FIRSTNAME" SIZE="20" MAXLENGTH="50">
<SELECT NAME="Department_ID">
<OPTION VALUE="#Department_ID#"> #Department_Name# </OPTION>
<B>Employee Start Date(mm/dd/yy)</B><BR> <INPUT TYPE="Text" NAME="StartDate" size="16" maxlength="16">
<B>Employee Salary (75000)</B><BR> <INPUT TYPE="Text" NAME="Salary" size="10" maxlength="10">
<INPUT TYPE="Submit" NAME="SubmitButton" VALUE="Add Employee">
InsertAction.cfm
.You will need to have this page created in order to test form validation.
You will add the code needed for inserting data in the next procedure.
InsertForm.cfm
in a browser.Click here to see the how the form should look and validate.
Click here to see InsertForm.cfm
's code.
Move on to the next procedure to build the action page that inserts the data into the database.
![]() |
Note: | Almost all links will work at this time. You will learn how to define a checkbox value for the action page query during the next procedure. |