0

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?

1

2 Answers 2

4

You can use the depends

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

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

It has to be the other way right round. If you check the checkbox, then the input field hast to be required
0

Add "required" keyword to your element:

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

1 Comment

I have to do it with the class="form-input validate required", because its a own style. And the textfield should only get this class when the checkbox is checked...

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.