March 11, 2010

Pages


Search Site


Subscribe

...to receive future posts via email.

Topics



Archives

Override CFSelect validation so that it "Just Works"

July 24 2008 by Adam

It's always bothered me that CFSelect validation only worked for multiple-select (list) boxes, and that we were left to our own devices to validate single-select (drop down) inputs. A few weeks ago I found a comment on Ben Forta's Blog that describes how to override the default validation function to properly handle single selects. The one caveat is that, since by nature a single-select input will always have one option selected, you have to have one invalid option. Generally I make it display, "Choose One:" or something like that. The value must be "" (not even a space).

To override the validation function, simply include this in your JavaScript somewhere. I put it in my global JS include so I never have to think about whether or not I'll need it on a given page.

var _CF_hasValue_old = _CF_hasValue; _CF_hasValue=function(_b,_c,_d){ if (_b.type == 'select-one'){ var bSelected = false; for (var i=0; i<_b.options.length; i++){ if ( _b.options[i].selected == true && _b.options[i].value != '' ) { bSelected = true; break; } } return bSelected; }else{ return _CF_hasValue_old(_b,_c,_d); } }

Use it like so:

Again, I can't take credit for coding this. I found it on Ben's blog, and thought it should make its way into the various aggregators.

Posted in ColdFusion | JavaScript | 0 comments

Loading Tweetbacks...

0 comments:

Leave a Reply





Leave this field empty: