Forum Moderators: open

Message Too Old, No Replies

Radio buttons required in a form

Making the visitor select a radio button (important)

         

johny_rm

6:04 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



hello,

I'm new to this forum but I'm not a total newbie with HTML. Nevertheless, I can't figure out how to make a visitor select a radio button.

CGI scripts (I've tried several) which process the results, somehow overlook the required tags with radio buttons. The script probably just checks if there are values defined/assigned, and radio buttons already have them.

Example 1:

<input type="text" name="name" size=20> 

If I make the field name required, and the visitor doesn't enter anything, he gets a warning, has to return to the previous page, and enter his/her name.

Example 2:

<input type="radio" name="gender" value="m">
<input type="radio" name="gender" value="f">

Here, however, even if I make the field gender required, and the visitor doesn't enter anything, the form gets processed nevertheless.

I did an extensive search on this topic, and the best advice I could find was 'use a javascript validator'. I'm not really familiar with javascripts, and also there are really a lot of questions on my website which use the radio buttons so the code shouldn't be too long.

The website I need this for is used for a research in which the visitors can participate. It is combined of 10 pages; and since the majority of questions use the radio buttons, I really would need some suggestions how to make the buttons really required.

The 'solution' of using other form types (list,text) is not acceptable in this case.

I appreciate any suggestions...
-

johny_rm

11:55 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



OK I've read and tested the javascript validate script found at [webmasterworld.com...]


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html">
<script language="javascript">
function checkFields() {
emptyfields = "";
if (document.form1.textbox1.value == "") {
emptyfields += "\n - Textbox1";
}
if(document.form1.textarea1.value == "") {
emptyfields += "\n - Textarea1";
}
if (document.form1.password1.value == "") {
emptyfields += "\n - Password1";
}
var clickedButton = "";
for(i=0;i<3;i++){
if(document.form1.radio1[ i ].checked == true){
clickedButton = "true";
break
}
}
if (clickedButton == "") {
emptyfields += "\n - radio1";
}
if (document.form1.checkbox1.checked == false) {
emptyfields += "\n - Checkbox1";
}
if (document.form1.select1.options[document.form1.select1.selectedIndex].value == "") {
emptyfields += "\n - Select1";
}

if (emptyfields!= "") {
emptyfields = "These fields are required:\n" +
emptyfields + "\n\nPlease fill in all required feilds";
alert(emptyfields);
return false;
}
else return true;
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form action="" name="form1" onsubmit="return checkFields()">
<input type="text" name="textbox1"><br>
<textarea cols="10" rows="5" name="textarea1"></textarea><br>
<input type="password" name="password1"><br>
<input type="radio" name="radio1" value="radio11">
<input type="radio" name="radio1" value="radio12">
<input type="radio" name="radio1" value="radio13"><br>
<input type="checkbox" name="checkbox1" value="checkbox1"><br>
<select name="select1"">
<option value="">Make a Selection</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br>
<input type="submit" name="submit1" value="submit1"><br>
</form>
</body>
</html>

I guess I'll have to do a lot of extra coding BUT I at least need some help with the buttons - the current javascript only does one radio set, and I can't figure it out how to check more sets (more questions). I've done some random testing, but it seems too difficult for now.

johny_rm

10:35 pm on Mar 8, 2004 (gmt 0)

10+ Year Member



I had to change the names of each radio check function on three times and I only did that twice...

Sorry for posting stupid questions :)