Mariam
Mariam

Reputation: 749

Ruby on Rails - Simple form client side validation that response was entered before submit

I am trying to do a very simple validtion on a form with only radio buttons to check that a user selected an option on the question before submitting the page. Is there a simple way to do this in my view other than relying on jquery validation or doing the validation in the model?

Mariam

Upvotes: 0

Views: 1374

Answers (2)

Naren Sisodiya
Naren Sisodiya

Reputation: 7288

you can write your own validation function and call it on form submit event, the function checks the radio button and should return true or false based on condition.

<form onsubmit="return validateForm(this)">

<input type="radio" name="radio_param" value="val  1" />
<input type="radio" name="radio_param" value="val  2" />
<input type="radio" name="radio_param" value="val  3" />
</form>

<script>
function validateForm(f){

var radioButtons = f.radio_param;
var selected = false;
for(var i = 0;i< radioButtons.length;i++){
 selected = radioButtons[i].checked;
 }
if(!selected)
 alert("no option selected");
return selected ;
}
</script>

Upvotes: 1

Mike Lewis
Mike Lewis

Reputation: 64177

If you want to do client side validation, you need javascript to handle this, plain and simple. You don't have to do rely on jQuery but it does provide some validation niceties.

Upvotes: 0

Related Questions