tags:

views:

115

answers:

2

The question is on the client side validation using java script.

Significant parts of the program related to the question are given below..

...
<form name="results" action="" method="post">
...
<input type="radio" name="id" value="<%= id_edit %>" /> <!-- Don't bother about id_edit -->
....
<input type="button" name="Edit" value="Edit" onclick="invoke(0)"><input type="button" name="Delete" value="Delete" onclick="return invoke(1)">
....

The script,

function invoke(btn)
{   
    if(btn == 0) document.results.action="gev.do";
    if(btn == 1) document.results.action="del.do";
    document.results.submit(); 

}

What basically i do is, depending on the selection of button, i pass a value to the servlet, either for updation or deletion.

My question is, how can i validate the radiobutton, ie, i can any one help me out writing the js to validate if any one of the radio button is selected.

All my efforts are in vein and i now use server side validation.

A: 

Use the checked attribute for checking the state of a radio button.

code_burgar
A: 
function at_least_one_checked(radio_group) {
  for (var i = 0, j = radio_group.length; i<j; i++) {
    if (radio_group[i].checked) {
      return true;
    }
  }
  return false;
}

if ( at_least_one_checked(document.forms.myForm.elements.myGroup) ) {
  // …
}
David Dorward
Is there a typo in there? As in should be i < radio_group.length rather than j = radio_group.length?
mikej
There was a typo, but not quite that one. Fixed it now. Thanks.
David Dorward