Nick Ginanto
Nick Ginanto

Reputation: 32130

Submit a form only if a checkbox is checked

I want to submit a form only if a checkbox is checked (as opposed to submit a form via the checkbox)

I don't need to save the checking of the checkbox. It is similar to "I agree to EULA bla bla"

Prefer not using javascript for this

thanks

Upvotes: 2

Views: 2572

Answers (2)

Nick Ginanto
Nick Ginanto

Reputation: 32130

I've solved the issue using a validates :terms_of_service, :acceptance => true in the model and adding a checkbox in the form block.

That solved that issue quick and easy.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382696

You can check if checkbox is checked or not using code like this:

var chkbox = document.getElementById('checkboxID');

if (chkbox.checked === true){
  // it is checked, submit form
}
else {
  // it is not checked, dont submit form
}

Upvotes: 2

Related Questions