For the complete experience, please enable JavaScript in your browser. Thank you!

  • Creative Cloud
  • Photoshop
  • Illustrator
  • InDesign
  • Premiere Pro
  • After Effects
  • Lightroom
  • See all
  • See plans for: businesses photographers students
  • Document Cloud
  • Acrobat DC
  • eSign
  • Stock
  • Elements
  • Marketing Cloud
  • Analytics
  • Audience Manager
  • Campaign
  • Experience Manager
  • Media Optimizer
  • Target
  • See all
  • Acrobat Reader DC
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player
  • All products
  • Creative Cloud
  • Individuals
  • Photographers
  • Students and Teachers
  • Business
  • Schools and Universities
  • Marketing Cloud
  • Document Cloud
  • Stock
  • Elements
  • All products
  • Get Support
    Find answers quickly. Contact us if you need to.
    Start now >
  • Learn the apps
    Get started or learn new ways to work.
    Learn now >
  • Ask the community
    Post questions and get answers from experts.
    Start now >
    • About Us
    • Careers At Adobe
    • Investor Relations
    • Privacy  |  Security
    • Corporate Responsibility
    • Customer Showcase
    • Events
    • Contact Us
News
    • 3/22/2016
      Adobe Summit 2016: Are You An Experience Business?
    • 3/22/2016
      Adobe Announces Cross-Device Co-op to Enable People-Based Marketing
    • 3/22/2016
      Adobe and comScore Advance Digital TV and Ad Measurement
    • 3/22/2016
      Adobe Marketing Cloud Redefines TV Experience
Validating Data-Developing guide / 

Validating form input and handling errors with JavaScript

Adobe Community Help


Applies to

  • ColdFusion

Contact support

 
By clicking Submit, you accept the Adobe Terms of Use.
 

ColdFusion lets you write your own validation routines in JavaScript, and lets you create JavaScript error handlers.

Validating input with JavaScript

In addition to native ColdFusion input validation using the validate attribute of the cfinput and cftextarea tags, the following tags support the onValidate attribute, which lets you specify a JavaScript function to handle your cfform input validation:

  • cfgrid
  • cfinput
  • cfslider
  • cftextarea
  • cftree
    ColdFusion passes the following arguments to the JavaScript function that you specify in the onValidate attribute:
  • The form JavaScript DOM object
  • The name attribute of the form element
  • The value of the control to validate
    For example, if you write the cfinput tag as the following:

<cfinput type="text"
...
<!--- Do not include () in JavaScript function name. --->
onvalidate="handleValidation"
...
>

You define the JavaScript function as the following:

<script>
<!--
function handleValidation(form_object, input_object, object_value) {
...
}
//-->
</script>

Example: validating a password

The following example validates a password. The password must have at least one of each of the following: an uppercase letter, a lowercase letter, and a number. It must be from 8 through 12 characters long. If the password is invalid, the browser displays a message box. If the password is valid, it redisplays the page with a brief success message.

Use JavaScript to validate form data
  1. Create a ColdFusion page with the following content:

    <html>
    <head>
    <title>JavaScript Validation</title>

    <!--- JavaScript used for validation. --->
    <script>
    <!--
    // Regular expressions used for pattern matching.
    var anUpperCase = /[A-Z]/;
    var aLowerCase = /[a-z]/;
    var aNumber = /[0-9]/;
    /* The function specified by the onValidate attribute.
    Tests for existence of at least one uppercase, lowercase, and numeric
    character, and checks the length for a minimum.
    A maximum length test is not needed because of the cfinput maxlength
    attribute. */
    function testpasswd(form, ctrl, value) {
    if (value.length < 8 || value.search(anUpperCase) == -1 ||
    value.search (aLowerCase) == -1 || value.search (aNumber) == -1)
    {
    return (false);
    }
    else
    {
    return (true);
    }
    }
    //-->
    </script>
    </head>

    <body>
    <h2>JavaScript validation test</h2>
    <!--- Form is submitted only if the password is valid. --->
    <cfif IsDefined("Form.passwd1")>
    <p>Your Password if valid.</p>
    </cfif>
    <p>Please enter your new password:</p>

    <cfform name="UpdateForm" preservedata="Yes" >
    <!--- The onValidate attribute specifies the JavaScript validation
    function. The message attribute is the message that appears
    if the validation function returns False. --->
    <cfinput type="password" name="passwd1" required="YES"
    onValidate="testpasswd"
    message="Your password must have 8-12 characters and include uppercase
    and lowercase letters and at least one number."
    size="13" maxlength="12">

    <input type="Submit" value=" Update... ">
    </cfform>

    </body>
    </html>

  2. Save the page as validjs.cfm.
  3. View the validjs.cfm page in your browser.

Handling failed validation

The onError attribute lets you specify a JavaScript function to execute if an onValidate, onBlur, or onSubmit validation fails. For example, if you use the onValidate attribute to specify a JavaScript function to handle input validation, you can also use the onError attribute to specify a JavaScript function to handle a failed validation (that is, when onValidate returns a False value). If you use the onValidate attribute, you can also use the onError attribute to specify a JavaScript function that handles the validation errors. The following cfform tags support the onerror attribute:

  • cfgrid
  • cfinput
  • cfselect
  • cfslider
  • cftextinput
  • cftree
    ColdFusion passes the following JavaScript objects to the function in the onerror attribute:
  • The JavaScript form object
  • The name attribute of the form element
  • The value that failed validation
  • The error message text specified by the CFML tag's message attribute
    The following example shows a form that uses an onError attribute to tell ColdFusion to call a showErrorMessage JavaScript function that uses the alert method to display an error message. The function assembles the message from the invalid value and the contents of the cfinput tag's message attribute.
<!--- The JavaScript function to handle errors.
Puts a message, including the field name and value, in an alert box. --->
<script>
<!--
function showErrorMessage(form, ctrl, value, message) {
alert("The value " + value +" of the " + ctrl + " field " + message);
}
//-->
</script>

<!--- The form.
The cfinput tags use the onError attribute to override the ColdFusion
default error message mechanism. --->
<cfform>
<!--- A minimum quantity is required and must be a number. --->
Minimum Quantity: <cfinput type="Text" name="MinQuantity"
onError="showErrorMessage" validate="numeric" required="Yes"
message="is not a number." ><br>
<!--- A maximum quantity is optional, but must be a number if supplied. --->
Maximum Quantity: <cfinput type="Text" name="MaxQuantity"
onError="showErrorMessage" validate="numeric"
message="is not a number." ><br>
<cfinput type="submit" name="submitit">
</cfform>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy

Choose your region United States (Change)   Products   Downloads   Learn & Support   Company
Choose your region Close

Americas

Europe, Middle East and Africa

Asia Pacific

  • Brasil
  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Cyprus - English
  • Česká republika
  • Danmark
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Greece - English
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Malta - English
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Southeast Asia (Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam) - English
  • 台灣

Commonwealth of Independent States

  • Includes Armenia, Azerbaijan, Belarus, Georgia, Moldova, Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan, Ukraine, Uzbekistan

Copyright © 2016 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

AdChoices