Mike Vierwind
Mike Vierwind

Reputation: 1520

Make a validation with checkbox

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

Upvotes: 0

Views: 197

Answers (1)

Ram
Ram

Reputation: 144659

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')
})

Upvotes: 1

Related Questions