0

I am new in javascript. I want a validation on my checkbox. I have this html

<input type="checkbox" name="acceptTerms"/>
<input type="submit" name="subscribe" value="Enter now" title="Enter now" />

When you submit, and you not select the checkbox. The checkbox must get the class "error". When you check the checkbox. Than the form must be sumit. How can make that with jQuery / js?

Thanks

0

1 Answer 1

1

Try the following:

$('#yourForm').submit(function(e){
    var $check = $('input[name=acceptTerms]');
    if (!$check.is(':checked')) {
       $check.addClass('error');
       e.preventDefault()
    }
})

$('input[name=acceptTerms]').change(function(){
     if (this.checked) $(this).removeClass('error')
})
Sign up to request clarification or add additional context in comments.

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.