Forums

Validation: how to set field mendotory clientside

Melgui 19 Apr, 2012
Dear members, dear support team,

would you please give me a tip how to set field mendotory if other field has value xy.
And this client side (on button click).

Here my issue:


Thanks a lot!
Alex
GreyHead 21 Apr, 2012
Hi Alex,

Here is one possible solution. It uses the fact that disabled inputs aren't validated.

The form has the radio button group with the name q1 and the input with the id 'other_1' set with validation 'required'.

This script is in a Load JS action:
window.addEvent('domready', function() {
    var q1, other;
    q1 = $$('#q1_container_div input');
    other = $('other_1');
    other.disabled = true;
    other.value = '';
    q1.each(function(item) {
        item.addEvent('click', function(){
            var other = $('other_1');
            if ( this.value == '3' && this.checked ) {
                other.disabled = false;
            } else {
                other.value = '';
                other.disabled = true;
            }
        });
    });
});

There a function that runs when the radion buttons are clicked, if radio 3 is checked then the 'other' input is enabled; otherwise it is cleared and disabled.

Bob
Melgui 23 Apr, 2012
Hi Bob,

it's great!!! You solution works! You are the best.
Thanks a lot!
Melgui 23 Apr, 2012
Hi Bob, one question more, the last, really🙂!

How can I do the same with combobox selection?

this.checked = this.selected???

q1.each(function(item) {
item.addEvent('click', function(){
var other = $('other_1');
if ( this.value == '3' && this.checked ) {
other.disabled = false;
} else {
other.value = '';
other.disabled = true;
}
});

Pleaase, help.
Thanks
Melgui 24 Apr, 2012
Hi Bob,

I have soledv myself!

All, what I have changed was "input->select"

window.addEvent('domready', function() {
    var sl, other;
    sl= $$('#id_container_div select');
    other = $('id1');
    other.disabled = true;
    other.value = '';
    sl.each(function(item) {
        item.addEvent('click', function(){
            var other = $('id1');
            if ( this.value == '6') {
                other.disabled = false;
            } else {
                other.value = '';
                other.disabled = true;
            }
        });
    });
});

Thank you!
GreyHead 24 Apr, 2012
Hi Melgui,

Well done, you got to the answer before I did :-)

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