admin管理员组

文章数量:1418148

I use Jquery Validation to validate my form.

I have following HTML-form:

<form id="caller">
    <label>Phone:</label>
<input type="text" name="phone" id="phonen" class="form-input" value="" />

    <div class="option-group check">
    <input type="checkbox" name="check1" id="check1"  value="1"  />
<label for="check1"><p>Need a call</p></label>          
</div></form>

Javascript-Code:

<script type="text/javascript">
 $(document).ready(function(e) {
     $('#caller').validate({
        errorPlacement: function(error, element){}
    });
    });
 </script>

If the checkbox is checked, text should get class="form-input validate required".

How can I do this?

I use Jquery Validation to validate my form.

I have following HTML-form:

<form id="caller">
    <label>Phone:</label>
<input type="text" name="phone" id="phonen" class="form-input" value="" />

    <div class="option-group check">
    <input type="checkbox" name="check1" id="check1"  value="1"  />
<label for="check1"><p>Need a call</p></label>          
</div></form>

Javascript-Code:

<script type="text/javascript">
 $(document).ready(function(e) {
     $('#caller').validate({
        errorPlacement: function(error, element){}
    });
    });
 </script>

If the checkbox is checked, text should get class="form-input validate required".

How can I do this?

Share Improve this question asked Dec 14, 2013 at 8:43 user3061007user3061007 572 silver badges4 bronze badges 1
  • Try first reading the docs before asking: jqueryvalidation/documentation – Hardy Commented Dec 14, 2013 at 8:45
Add a ment  | 

2 Answers 2

Reset to default 4

You can use the depends

$('#caller').validate({
    rules:{
        phone:{
            required: {
                depends: function(){
                    return $('#check1').is(':checked')
                }
            }
        }
    }
});

Demo: Fiddle

Add "required" keyword to your element:

<input type="text" name="validate-me" required />

本文标签: