Making a single cfselect required

I’ve experienced the same frustration as others when it came to requring a value for a cfselect.  The documentation states that you just have to set the required attribute of the cfselect to true and it should validate.  This unfortunately only works for cfselects that allow multiple values (size >= 2).  For a single cfselect, a value is always selected and the requirement will never be properly triggered.  I’ve tried numerous workarounds that always failed to produce the appropriate results.  I finally came across the solutions today, posted from a user on the Adobe Coldfusion MX7 LiveDocs for cfselect.  The solution was so ridiculously simple that I couldn’t help but laugh at myself.  The user pointed out to use the following javascript:

 

<script language="JavaScript"><!–

document.formName.selectName.selectedIndex=-1;

//–>

</script>

Place this script after your cfselect with the required attribute set to true, and it will validate upon form submission.  I have tested this on HTML forms only.  I’ll test later on Flash forms and post my results.

Hope this helps.  Happy Coding!

Leave a comment