Forums

Required Field Based On Condition

guillome 01 Nov, 2014
Hello,

is it possible to enable or disable a fields required validation based on another checkbox that is chosen?
I would like to make something required whenever someone checks one value in another field.

thanks in advance,
Gery
GreyHead 02 Nov, 2014
Hi Gery,

In CFv5 I believe that you can do this by adding / removing the validation class using JavaScript.

Bob
guillome 02 Nov, 2014
Thanks, how can I do it?
GreyHead 03 Nov, 2014
Hi Gery,

It will be something like this:
jQuery(document).ready(function (jQ) {
  jQ('#checkbox_id').click(function () {
    if ( jQ('#checkbox_id')..prop('checked') {
      jQ('#field_id').addClass('validate[\'required\']');
    } else {
      jQ('#field_id').removeClass('validate[\'required\']');
    }
  });
});
!!! Not tested and may need debugging !!!

Bob
This topic is locked and no more replies can be posted.