Forums

dyamic validation for a field

elvinjoe 14 Dec, 2011
Hi,

I want a field to be shown/hidden and validated depending on a value of a certain dropdown.
I am able to hide/show the field but the validation would not work.

I have added this code in my load JS event. Have i missed something?


window.addEvent('domready', function() {
    $('product_interest').addEvent('change', function() {
        if($('product_interest').value == 'Loans'){
        	$('loanspacer').setStyle('display', '');
        	$('loanrow').setStyle('display', '');
            $('comments').addClass("validate['required']");
            formCheck.register($('comments'));
		}
    });
});
elvinjoe 15 Dec, 2011
formCheck.register($('comments'));

is working since when i replace it with a non existing field it will generate a javascript error.
GreyHead 15 Dec, 2011
Hi elvinjoe ,

That looks OK to me. Please take a Form Backup using the icon in the Forms Manager and post it here (as a zipped file) or PM or email it to me and I'll take a closer look.

Bob
GreyHead 19 Dec, 2011
Hi elvinjoe,

This works OK on my test site. The only problem is that it comes last in the validation sequence. You can fix this with a second parameter.
formCheck.register($('comments'), 1);


I also added code to handle a change of the drop-down and the page reloading:
window.addEvent('domready', function() {
  $('product_interest').addEvent('change', function() {
    if ( $('product_interest').value == 'Loans' ) {
      $('loanspacer').setStyle('display', 'table-row');
      $('loanrow').setStyle('display', 'table-row');
      $('comments').addClass("validate['required']");
      formCheck.register($('comments'), 1);
    } else {
      $('comments').removeClass("validate['required']");
      formCheck.dispose($('comments'));
      $('loanspacer').setStyle('display', 'none');
      $('loanrow').setStyle('display', 'none');
    }
  });
});
window.addEvent('load', function() {
  if ( $('product_interest').value == 'Loans' ) {
    $('loanspacer').setStyle('display', 'table-row');
    $('loanrow').setStyle('display', 'table-row');
    $('comments').addClass("validate['required']");
    formCheck.register($('comments'), 1);
  }
});

Bob
jhdesign 26 Jan, 2012
Wonder if you can help me with similar issue.

on the addEvent, I keep getting the VARNAME is null.

Tried $('#radio5').value as well as $('radio5').value but keep getting "radio5 is null"

Mootools is loaded.
GreyHead 30 Jan, 2012
Hi jhdesign ,

Check that you have an input with the id='radio5' set??

Bob
jhdesign 30 Jan, 2012
Bob yes it here, here's the code:


Yes
  <input type="radio" name="radio5" id="radio50" class="radio" title="" value="Yes"/>
  
  No
  <input type="radio" name="radio5" id="radio51" class="radio" title="" value="No"/>


I have some jQuery on this page, I wonder if it is causing a conflict.
Max_admin 31 Jan, 2012
Your ids are "radio50" and "radio51", so "radio5" will be null for sure.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.