开发者

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

开发者 https://www.devze.com 2023-02-25 15:34 出处:网络
I am trying to do a very开发者_如何转开发 simple validtion on a form with only radio buttonsto check that a user selected an option on the question before submitting the page. Is there a simple way to

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


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消