Validation in Coldfusion Forms
The first thing you'll need to do is convert all those crusty INPUT tags to CFINPUT. The CFINPUT tag is one of those extended controls that ColdFusion users adore. It has two extremely flavorful options: validation and requirement. You already know what validation means. Requirement means that if information is not valid, an alert box will honk at you until good data is entered. Let's make the simple changes to that will get this done.

You can add validation to your form manually. All you have to do is add three attributes to your CFINPUT tag: REQUIRED, VALIDATE, and MESSAGE. If you set Required to "Yes," the form won't submit if the field is blank. If users try to do it anyway, they 'll get whatever you put in the MESSAGE attribute. VALIDATE lets you check to ensure that the form input is in a certain format. If it's not, you'll get the MESSAGE attribute. Available formats are integer, long, date, eurodate, time, telephone, ZIP code, credit card, and Social Security number.

If we were going to add validation and requirement to the phone field, our code would look like this:




type="Text"

name="phone"

message="Please enter your phone number in XXX-XXX-XXXX format."

validate="telephone"

required="Yes"

size="12"

maxlength="12">


You only need to change the input tags to cfinput that you need to validate. Don't forget to change the closing tag to /cfform. Also, remove and cfoutput tags from inside the cfinput tags as they are not needed. Also remember to take out the cfoutput tags in the cfform action= parameter.

Archives