View comments | RSS feed

cfinput

Description

Used within the cfform tag, to place radio buttons, check boxes, or text boxes on a form. Provides input validation for the specified control type.

Category

Forms tags

Syntax

<cfinput 
  type = "input_type"
  name = "name"
  value = "initial_value"
  required = "Yes" or "No"
  range = "min_value, max_value"
  validate = "data_type"
  onValidate = "javascript_function"
  pattern = "regexp"
  message = "validation_msg"
  onError = "text"
  size = "integer"
  maxLength = "integer"
  checked 
  passThrough = "HTML_attributes">

See also

cfapplet, cfform, cfgrid, cfselect, cfslider, cftextinput, cftree

History

New in ColdFusion MX: if the cfform tag preserveData attribute is set to True, ColdFusion checks radio and checkbox values only if their value matches the posted value for the control. (In earlier releases, if the posted value did not match any of the cfinput checkboxes or radio buttons for the control, the checked attribute was used.

Attributes

Attribute Req/Opt Default Description
type
Optional
text
  • text: creates a text entry box control
  • radio: creates a radio button control
  • checkbox: creates a checkbox control
  • password: creates a password entry control
name
Required

Name for form input element.
value
Optional

Initial value for form input element.
required
Optional
No
  • Yes
  • No
range
Optional

Minimum and maximum value range, separated by a comma. If type = "text" or "password", this applies only to numeric data.
validate
Optional

Verifies a value's format:
  • date: US date mm/dd/yyyy
  • eurodate: European date dd/mm/yyyy
  • time: time hh:mm:ss
  • float: floating point entry
  • integer: integer entry
  • telephone: telephone ###-###-####. Separator: hyphen or blank. Area code and exchange must begin with a digit 1 - 9.
  • zipcode: (U.S. formats only) 5-digit ##### or 9-digit #####-####. Separator: hyphen or blank.
  • creditcard: strips blanks and dashes; uses the mod10 algorithm.
  • social_security_number: ###-##-####. Separator: hyphen or blank.
  • regular_expression: matches input against regular expression specified by the pattern attribute.
onValidate
Optional

Custom JavaScript function to validate user input. The form object, input object, and input object values are passed to the routine, which should return True if validation succeeds, and False otherwise. If used, the validate attribute is ignored.
pattern
Required if validate = "regular_
expression"


JavaScript regular expression pattern to validate input. Omit leading and trailing slashes. For examples and syntax, see the "Building Dynamic Forms" chapter in Developing ColdFusion Applications.
message
Optional

Message text to display if validation fails.
onError
Optional

Custom JavaScript function to execute if validation fails.
size
Optional

Size of input control. Ignored, if type = "radio" or "checkbox".
maxLength
Optional

Maximum length of text entered, if type = "Text" or "password".
checked
Optional

Selects a control. No value is required.
Applies if type = "radio" or "checkbox".
Optional: you can enter the following values:
  • true (equivalent to checked)
  • false (equivalent to omitting the attribute)
passThrough
Optional

HTML attributes that are not supported by cfinput. If you specify an attribute and value, they are passed to the HTML code generated for the tag.

Usage

If the cfform preserveData attribute is true and the form posts back to the same page, the posted value of the cfinput control is used, instead of its Value or Checked attribute.

If cfinput checkbox or radio type values match the submitted value for the control, ColdFusion checks their values. If no value matches, nothing is checked.

To add other HTML <input> tag attributes and values to this tag, use the passThrough attribute. They are passed through ColdFusion to the browser when creating a form. The supported HTML attributes are: CLASS, ID, MAXLENGTH, MESSAGE, ONBLUR, ONCHANGE, ONCLICK, ONDBLCLICK, ONFOCUS, SIZE, STYLE, and TABINDEX.

If you specify a value in quotation marks, you must escape them; for example,

passThrough = "readonly = " "YES " " "

For more information, see "cfform". For information on using JavaScript regular expressions with this tag, see Developing ColdFusion MX Applications with CFML.

Example

<!--- this example shows the use of cfinput within a cfform to ensure simple
validation of text items --->
<cfform action = "cfinput.cfm">
<!--- phone number validation --->
Phone Number Validation (enter a properly formatted phone number): <br>
<cfinput 
  type = "Text" name = "MyPhone" 
  message = "Enter telephone number,formatted xxx-xxx-xxxx (e.g. 617-761-2000)"
  validate = "telephone" required = "Yes">
  <font size = -1 color = red>Required</font>
<!--- zip code validation --->
<p>Zip Code Validation (enter a properly formatted zip code):<br>
<cfinput 
  type = "Text" name = "MyZip" 
  message = "Enter zip code, formatted xxxxx or xxxxx-xxxx" 
  validate = "zipcode" required = "Yes">
  <font size = -1 color = red>Required</font>
<!--- range validation --->
<p>Range Validation (enter an integer from 1 to 5): <br>
<cfinput 
  type = "Text" name = "MyRange" range = "1,5" 
  message = "You must enter an integer from 1 to 5" 
  validate = "integer" required = "No">
<!--- date validation --->
<p>Date Validation (enter a properly formatted date):<br>
<cfinput 
  type = "Text" name = "MyDate" 
  message = "Enter a correctly formatted date (dd/mm/yy)" 
  validate = "date" required = "No">
<input 
  type = "Submit" name = "" 
  value = "send my information">
</cfform>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


fusebox_steve said on Jul 1, 2002 at 10:22 PM :
cfinput should have an ending tag and allow for a type=textarea
ctina said on Jul 22, 2002 at 2:37 PM :
Update from Macromedia, in response to fusebox_steve: thanks for your suggestion. I've added it to the ColdFUsion enhancements request list.
hlichtin said on Mar 7, 2003 at 3:11 PM :
The check requires 16 digits in the number.
lolozigzag said on Jun 6, 2003 at 3:03 PM :
I am using cfinput type="file" for the form side of the cffile action, but I don't see that type as an option on this page.
kwokie said on Mar 5, 2003 at 2:00 PM :
Credit card validation seems to only look for a 20-number credit card, but AMEX cards have less than 20 numbers.
mschweiz said on Jul 10, 2003 at 12:51 PM :
I just copied the above script and trie it on my webserver with ColdFusion MX server.
The required fields are not checked.
The TAGs <cfform> and <cfinput> are not usable at all.
Steve Powell said on Sep 1, 2003 at 1:11 AM :
<CFFORM> seems to be
preventing me from using
my enter key to create
multi-line text in a
TEXTAREA
No screen name said on Sep 30, 2003 at 6:06 PM :
You need to have a way to enter a canadian validated postal code <a1b 1a2>
jrunrandy said on Oct 24, 2003 at 6:32 AM :
I have entered CFINPUT Canadian Postal Code validation as enhancement request 53631.
No screen name said on Apr 5, 2004 at 1:13 PM :
email field validation should be available
Hutz said on Dec 8, 2004 at 7:20 AM :
Validating on a "regular_expression" seems to then ignore the absence of a "required" -- ie: if you don't have REQUIRED="Yes" it still tries to validate an empty field. Even using REQUIRED="No" does not keep it from trying to validate an empty field.
No screen name said on Dec 19, 2004 at 10:33 AM :
Well, if you are writing a function to validate the input, check to see if it is blank, and if it is, perform a javascript "alert()". Come on, this isn't that hard.
And it says in the doc right above it behaves this way.
Hutz said on Apr 25, 2005 at 3:09 PM :
Let's be clear: you're NOT writing a function, you're using a new attribute added to the ColdFusion Tag (Validate="regular-expression"). This new attribute breaks other attributes in unexpected ways (ie: the "required" attribute is no longer respected). This is a bug, and it is not documented on this page except for my comment about it.

I was hoping this new attribute would allow me to stop having to use the special extra validation function I wrote for email, but alas, with this bug making it is not ready for production, the answer is "no".
Hutz said on Apr 25, 2005 at 3:33 PM :
And oh yeah: since then, I figured out a way around the problem by having the regular expression match a null field as well as what you're searching for if you want it to be not required. This is not intuitive, and I would still argue that the regular_expression validation attribute breaks the "required" attribute, but for example for emails, the following works to validate an email address if one is given, but not to require one:

VALIDATE="regular_expression" PATTERN="(.+@.+\.\w{2,6}$|^$)"
Mitch004 said on Jul 14, 2006 at 12:11 PM :
I've discovered the problem with the <cfinput validate="creditcard">.
In the cfform.js, the last line of the _CF_checkcreditcard function needs to flip the return true/false.

original:
return (checkdigit % 10) == 0 ? true : false;

should be
return (checkdigit % 10) == 0 ? false : true;

After this change, the validation worked perfect.
Mitch004 said on Jul 24, 2006 at 12:57 PM :
Disregard my previous posting. I was wrong about the change to the _CF_checkcreditcard function.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt161.htm