Adam
Adam

Reputation: 21

jquery validate conditional max

I have a form where there's an option group with the choice of entering a value in pounds or a percentage.

Following this I have an input box for the actual value or the percentage.

I want to use the validate functions in jquery to set a conditional maximum value (e.g.100) if the user chooses the percent option.

Is this possible - I tried this but it doesn't work:

sponsorValue: {
    required: true,
    digits: true,
    max: function(element) {
    if ($("#sponsorOption").val() == '2'){ return 100;}
                       }
        }

can anyone offer any help?

Upvotes: 2

Views: 2538

Answers (2)

eggwater
eggwater

Reputation: 162

This is how the html form radios are marked up:

<input name="sponsorOption" type="radio" value="1" /> <span class="stdTxt">Amount</span><br />
<input checked="checked" name="sponsorOption" type="radio" value="2" checked="checked"/> <span class="stdTxt">Percentage</span>

apologies for adding this as an answer my user wasn't set up yesterday.

Upvotes: 0

eggwater
eggwater

Reputation: 162

I think I may have cracked it:

sponsorValue: {
    required: true,
    digits: true,
    max: function(value,element) {
        if ($("input[name='sponsorOption']:checked").val() == '2') return 100;
               }
    }

I used a single checkbox instead of the radios in the end but I may try and retest using the radios.

Upvotes: 5

Related Questions