views:

58

answers:

3

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?

A: 

Try this...

$("#sponsorOption").val() == '2' ? 100 : null 
kekekela
thanks I'll give this a shot.
Adam
I tried your first idea - still doesn't seem to work though I get what you mean - I thought max was expecting a value - http://docs.jquery.com/Plugins/Validation/Methods/maxIs it possible to write a conditional statement within the rules?
Adam
@Adam - yeah, typically you just supply a value but when I saw your syntax I was thinking that was an overload that took a dependency expression like "required" has...but apparently it has no such thing...you should be able to do this with a ternary operator though, will update my answer
kekekela
hmmm still can't get this to play ball. Might have to use a different method rather than jquery.
Adam
@Adam noticed I had an extra paren in there (left over from removing the if statement) which I removed...also it could be that it doesn't like the null being passed back, I'd try setting it to Number.MAX_VALUE and see if that works
kekekela
I noticed the paren already - still no dice- :( I just wonder whether the function allows conditions in the max method. I might be trying to flog a dead horse here.
Adam
right I'm getting somewhere now: this is somewhat working in that the 'false' value element is definitely being called so it val() == '2' that isn't working - max: $("#sponsorOption").val() == '2' ? 100 : 20000
Adam
@Adam What's the html for sponsorOption look like?
kekekela
A: 

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.

Adam
I think I may need to make the ids sponsorOption1 and sponsorOption2
Adam
still bashing away at this. Have tried a lot of different variations. The problem seems to be that when the radio option is changed it doesn't update the value of the max. I tried to simply change the values of the radio buttons to match the thresholds required - e.g. 100 : 20000 and just use the value of the max as $("input[name='sponsorOption']:checked").val() - this didn't work either it just takes the intial default 'checked' value. very frustrating this.
Adam
+1  A: 

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.

Adam
This works with the radios too - in fact it works better - more functional.
Adam