CFFORM doesn't validate dropdown lists / combos even though required="true"

The Problem

Using javascript validation on a CFSELECT within a CFFORM on ColdFusion MX

The symptom

If a user doesn't select a value from a drop down list / combo then no validation occurs

The scenario

When using CFFORM to validate form fields, even though the validate="true" has been specified within the CFFORM, it fails to catch the fact that a value from the combo hasn't been chosen.

In the example below, if the user doesn't select either Male or Female from the drop down list, when you click the submit button the form will not be validated and the form will be submitted with a blank value in the form field specified in the CFSELECT.

<cfform action="postform.cfm">
  <cfselect name="gender" required="yes" message="Select you gender">
  <option value="">- Choose -</option>
  <option value="M">Male</option>
  <option value="F">Female</option>
  </cfselect>
  <br />
  <cfinput type="submit" name="go" value="Submit form">
</cfform>

 

The fix

To fix this problem you need to edit /CFIDE/scripts/cfform.js and add a tiny amount of extra code. NOTE: If you are in a hosted environment, you can use the scriptSrc parameter within the CFFORM tag. <cfform action="postform.cfm" scriptSrc="[your script directory here]"> This will allow you to upload this fix without your ISP having to modify their ColdFusion installation. Additionally, when they apply ColdFusion updates or hot fixes they changes may be overwritten.

Do a search in the source code for obj.options[i].selected - This should be around about line 78 (based on ColdFusion 7 - Version 8 is at line 57). You then need to replace the value you just searched for with the following:-

ColdFusion 7 instructions

Version 7 code to insert:-
if (obj.options[i].selected && obj.options[i].value != '')

Before:-

else if (obj_type == "SELECT")
{
for (i=0; i < obj.length; i++)
{
if (obj.options[i].selected)
return true;
}
return false;
}

After:- (VERSION 7)

else if (obj_type == "SELECT")
{
for (i=0; i < obj.length; i++)
{
if (obj.options[i].selected && obj.options[i].value != '')
return true;
}
return false;
}

ColdFusion 8 instructions:-

Version 8 code to insert:-
if (b.options[i].selected && b.options[i].value != '')

Before:-

return true;
}else{
if(_c=="SELECT"){
for(i=0;i<_b.length;i++){
if(_b.options[i].selected){
return true;
}

After:- (VERSION 8)

return true;
}else{
if(_c=="SELECT"){
for(i=0;i<_b.length;i++){
if (_b.options[i].selected && _b.options[i].value != '') {
return true;
}

 

 Key phrases:-

  • CFFORM validation
  • CFSELECT

Applies to / Tested on:-

  • ColdFusion MX7
  • ColdFusion 8
TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
When the 'size' attribute is set to '1', then it's simply impossible to _not_ select an option. By default the first option is automatically selected. The online ColdFusion documentation has this to say about the 'required' attribute of .

"This attribute has no effect if you omit the size attribute or set it to 1, because the browser always submits the displayed item. You can work around this issue: format forms by having an initial option tag with value=" " (note the space character between the quotation marks).""
# Posted By Adrock | 08/01/07 21:06
That's all well and groovy if you want multiple rows to show but when you want just a simple dropdown it's another matter.

I have just re-confirmed in my own mind, and setting a value=" " (with space) definitely doesn't work unless you specify (as you commented) size="2" (or above)

I stand by my kludge ;-) as it's the only way to get CFSELECT to validate as a combo rather than a list.
# Posted By Martin Parry | 09/01/07 01:04
By changing the CFIDE/scripts/cfform.js code as mentioned above, yes, you can get it to validate correctly using size=1 by putting an option in there that has a blank value: <option value="">--Select--</option>
# Posted By Marty McGee | 29/01/07 22:48
Cool - I'd like Adobe to include the fix in the next release! It's such a simple change and would save so much head scratching.
# Posted By Martin Parry | 05/02/07 22:54
Hello, just thought I would let you know that the syntax in your article is INCORRECT.

You have double quotes listed, but you need to have a double quote listed " ".

Reference Adobe livedocs here:
http://livedocs.adobe.com/coldfusion/5.0/CFML_Refe...
# Posted By Wesley Novack | 15/03/07 18:35
All I know is that the code works..
# Posted By Martin parry | 02/07/07 16:16
For CF8

The is Line 59 and the code is
if(_b.options[i].selected){
and change it to.....
if(_b.options[i].selected && _b.options[i].value != ''){

Somebody go smack the developers for not making this change standard!
# Posted By Charles PJ Higgins | 28/08/07 00:42
I know... I can't believe they've still not fixed it. Hey ho
# Posted By Martin Parry | 28/08/07 00:49
The CF8 Change didnt change this, and is still allowing it to submit. Any thoughts?
# Posted By Frank | 31/08/07 18:47
Have you tried implementing Charles' suggestions?
# Posted By Martin Parry | 01/09/07 22:23
I have now update the code examples above to include the CF8 CFFORM fix
# Posted By Martin Parry | 17/01/08 14:48
Thanks! Its worked for me in both CF7 and CF8.
# Posted By amy robinson | 22/01/08 18:41
Thanks!

I spent almost two weeks trying to figure this out with the help of Adobe CF Forum members. Using complex javascript, arrays, etc. Found this page on search and took care of the issue in five minutes!

Thanks again!
# Posted By Droogie | 06/03/08 19:44
I did the one for CF8 and for some reason it's not working for me. *confused* Here's what i got...
--- Line 55 - 61 ---
return true;
}else{
if(_c=="SELECT"){
for(i=0;i<_b.length;i++){
if(_b.options[i].selected && _b.options[i].value != ''){
return true;
}
# Posted By Korrupt | 10/03/08 05:51
Any chance there are any "kludges" for CF version 4.5?
# Posted By Jon | 31/03/08 04:07
Thanks! Worked like a charm! (CF8)
# Posted By noname | 08/04/08 03:59
Absolutely IDIOTIC that Adobe has allowed this issue to persist!
And the documentation gives you a workaround that doesn't work.

Come on!

Thanks for the code snippet.
# Posted By John Fitzgerald | 30/08/08 05:55
Thanks !!! This is work like a charm. Love it save me lots time.

thank you :)
# Posted By Jennifer | 18/09/08 21:23
Excellent resource, saved me hours of work, with just a quick google....

In my case, I modified it again so that it would validate if the selection was 'Please select...', i.e. the code was:

if (_b.options[i].selected && _b.options[i].value != 'Please select...') {

Instead of the above fix, this is standard right across the sites I am working on, so it shouldn't cause any issues, should it? Each site references it's own /scripts folder as well.
# Posted By Peter | 25/11/08 11:40
very thanks for you...
# Posted By Oyun | 26/11/08 21:50
Thanks !!! This is work like a charm. Love it save me lots time.

thank you :)
# Posted By Oyunlar | 01/04/09 19:53
Does anyone know if this was corrected in Version 9?
# Posted By Bob | 07/05/10 22:30
You have got to be kidding! I've struggled with this
for years.... then this SIMPLE fix answers the problem
in 2 minutes. GREAT JOB!
# Posted By larry | 09/05/10 23:44
awesome charm! fix already!
# Posted By mrzam | 19/05/10 04:13
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.002. Contact Blog Owner