views:

681

answers:

2

I am using this jQuery form validation plugin: http://docs.jquery.com/Plugins/Validation/Methods

I am trying to use the dependancy callback: http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-callback

What I am trying to do is only require "BAR" when "FOO" has a certain value.

Here's a basic example:

<select name="FOO" id="FOO">
  <option></option>
  <option value="someValue">some Value</option>
</select>

$("form[name=myForm]").validate({
   rules: {
     FOO: {required: true},
     BAR: {
             required: function(element) {
             return $("#FOO").val() == 'someValue';
     },
 maxlength: 10   
     }
   }  
});

But this is not working. I've also tried using :selected like so:

return $("#FOO :selected").val() == 'someValue';

Can anyone spot what the issue might be?

EDIT = Added the ID of FOO to the select

+1  A: 

The select element doesn't have id FOO.

You should either add it:

<select id="FOO" name="FOO">

Or make the right selection:

$('select[name=FOO]')
Daniel Moura
thanks a ton, I just saw that and updated... I updated the code.
jyoseph
AH! And actually now that I look at my code (the above was just an example) I see the select menu has no ID. Thanks for pointing that out, brain fart moment.
jyoseph
A: 
<script type="text/javascript">
jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});;
</script>

<script>
  $(document).ready(function(){
    $("#myform").validate({
  rules: {
    age: {
      required: true,
      min: 3
    },
    parent: {
      required: function(element) {
        return $("#age").val() < 13;
      }
    }
  }
});
$("#age").blur(function() {
  $("#parent").valid();
});


  });
  </script>

this must be work for you.

Sameera Thilakasiri