This section describes basic validation of form fields. Later sections in this chapter describe other validation types and techniques that you can use, including regular expression validation, masking, hidden field validation, JavaScript validation, and validation using CFML tags and functions.
In basic form field validation, you do the following:
The following example specifies onBlur validation of a telephone number:
Phone: <cfinput type="text" name="HPhone" validateat="onBlur" validate="required,telephone" message="Please enter a standard U.S. telephone number with an optional extension, such as x12345">
The following sections describe considerations for validation in cfinput and cftextarea tags, and show a more complete example.
General considerations: Consider the following issues when you determine how to validate form data:
Validation algorithm differences: The underlying validation code used when validating form data can differ depending on the validation technique and the form type. As a result, the algorithms used vary in some instances, including the following:
For detailed information on the validation algorithms used for validation techniques used on the server, see Validating form data using hidden fields.
If you create an XML skinnable form and use any skin provided by Adobe, such as the basic.xsl or silver.xsl skin, you can use all form validation techniques that are available for HTML forms.
If you use a custom skin (XSL file), the available validation techniques depend on the skin. The cf_webroot\CFIDE\scripts\xsl directory contains a _cfformvalidation.xsl file that implements all ColdFusion HTML form validation techniques and supports onBlur, onSubmit, onServer, and hidden form field validation. XML skin writers can include this file in their skin XSLT to implement ColdFusion validation for their skin.
The following form asks for information that might be used when registering a new user. It checks to make sure that the user enters required information. (Only the telephone number is optional.) It also checks to make sure that the telephone number and e-mail address are properly formatted and that the number to be used in a challenge question is in the proper range. This example performs onSubmit validation. It posts back to itself, and dumps the submitted results.
<cfif IsDefined("form.fieldnames")> <cfdump var="#form#"><br> </cfif> <cfform name="myform" preservedata="Yes" > First Name: <cfinput type="text" size="15" name="firstname" required="yes" message="You must enter a first name."><br> Last Name: <cfinput type="text" size="25" name="lastname" required="yes" message="You must enter a last name."><br> Telephone: <cfinput type="text" size="20" name="telephone" validate="telephone" message="You must enter your telephone number, for example 617-555-1212 x1234"><br> E-mail: <cfinput type="text" size="25" name="email" validate="email" required="Yes" message="You must enter a valid e-mail address."><br> Password: <cfinput type="password" size="12" name="password1" required="yes" maxlength="12" message="You must enter a password."><br> Reenter password: <cfinput type="password" size="12" name="password2" required="yes" maxlength="12" message="You must enter your password twice."><br> We will ask you for the following number, in the range 100-999 if you forget your password.<br> Number: <cfinput type="text" size="5" name="chalenge" validate="range" range="100,999" required="Yes" message="You must enter a reminder number in the range 100-999."><br> <cfinput type="submit" name="submitit"> </cfform>