0

I'm currently working with this; http://www.geektantra.com/2009/09/jquery-live-form-validation/

I'm trying to add validation so if a user checks the Location box, the "L2" field would have to be required. How can I achieve this with this plugin? Would I have to mix in the checkbox expression?;

expression: "if (isChecked(SelfID)) return true; else return false;",

Here's the plugin functions

<script type="text/javascript">
            jQuery(function(){
                jQuery("#v1").validate({
                    expression: "if (VAL) return true; else return false;",
                    message: "Please enter your name"
                });
            });
</script>

Here's the form

<form method="post" id="contactform" action="/form.php" >
<h5>Category:</h5>
<p><input type="checkbox" name="Location" value="Checked" onclick="toggle_visibility('f1');"> <span class="a1">- Location</span><br><div id="f1" style="display:none">Where are you located?<br /><input name="L2" id="L2" type="text"  /></div></p>

<h5>Name:</h5>
<p><input name="Name" id="v1" type="text" /></p>
<p><div><input name="submit" type="submit" value="Submit"/></div></p>
</form>
7
  • Use depends option. You can read it on the jquery validation - options page. Commented Jun 8, 2012 at 5:35
  • I would use addEventListener and attachEvent instead of the inline on attributes. Commented Jun 8, 2012 at 5:38
  • @Jules I'm pretty new to java, I checked out the dependency option, added this with id inline '$("#Location").blur(function() { $("#L2").valid(); });' I'm wondering since I'm using that plugin from geektantra, would that make a difference in terms of how the functions are? Commented Jun 8, 2012 at 5:52
  • @PhpMyCoder Could attachevent be used to combine the toggle visibility code + the validation method same as "v1"? So it acts as a onclick Commented Jun 8, 2012 at 6:07
  • @Jake Yes. In jQuery: $('input[name="Location"]').click(function() { toggle_visibility('f1'); }); Commented Jun 8, 2012 at 6:10

3 Answers 3

3

Do you mean:

rules: {
      fieldName: {
        required: {       
          depends: function() {
            return $("input[name='checkbox']:checked")
          }
        }
      }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I'm a newbie with javascript, would that work with the plugin though? I tried as a test '$("#myinput").rules("add", { minlength: 2 });' but that didn't seem to work
or can try this $("input[name='checkbox']").is(":checked") and remove element un-used variable
1

For some reason Sudhir's answer didn't work for me - I had to modify it:

rules: {
  fieldName: {
    required: {       
      depends: function(element) {
        return $("input[name='checkbox']:checked").length == 1
      }
    }
  }
}

Comments

0

Sudhir's answer is correct. All you have to do is use it as options when initialising the validation.

var opt = {};//copy from Sudhir's post of course change the field name as required.
$("form").validate(opt);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.