PDA

View Full Version : Checking with radio buttons


Flussig
03-04-2002, 10:51 AM
Hi there

Can anyone help me put a check on whether a radio button has been selected so that if the user doesn't select an option and then submits it throws back an error asking for a selection. Also, with one of the radio buttons (other) i would like to add a pop up text field for specifiying a description of other. This works by selecting the radio button called other and next to it pops up a text field to enter the other.

Many thanks

Jon Hanlon
03-04-2002, 04:18 PM
<form onsubmit="return ensureRadio(this)">
<b>What is your Overall Opinion of the forums?</b> <br>
<input type=radio name="overall" onclick="otherText(this.form)" value="great">Great!<br>
<input type=radio name="overall" onclick="otherText(this.form)" value="vgood">Pretty Good<br>
<input type=radio name="overall" onclick="otherText(this.form)" value="OK">OK I suppose<br>
<input type=radio name="overall" onclick="otherText(this.form)" value="other">Other <input type=text disabled name="overOther">
</form>

<script language="Javascript">
function otherText(formObj) {
var radioValue = getSetRadio(formObj.overall);
if (radioValue == "other") {
formObj.overOther.disabled = false;
formObj.overOther.focus();
} else {
formObj.overOther.value = "";
formObj.overOther.disabled = true;
}
}

function otherText(formObj) {
var radioValue = getSetRadio(formObj.overall);
if (radioValue) return true; // A-OK
alert("Choose a &$#@ing Radio button");
return false;
}

function getSetRadio(inObj) { // P2 is optional and implies we are Setting
var obj = getObject(inObj);
if (!obj) return false;
for (var i=0; i < obj.length; i++) {
if (arguments.length > 1) { // we are setting
if (arguments[1] == null) {
obj[i].checked = false;
} else
if (obj[i].value == arguments[1]) {
obj[i].checked = true;
return true;
}
} else { // we are getting
if (obj[i].checked) return obj[i].value;
}
}
return false;
}
</script>

Mark
03-04-2002, 04:18 PM
You need a form validation script!

See here for validating the whole form:
http://www.a1javascripts.com/essential_scripts/formvalidation/formvalidation.html

Or here for validationg a field in the form:
http://www.a1javascripts.com/essential_scripts/field_validation/field_validation.html

Flussig
03-05-2002, 08:02 AM
Thanks John, but i seem to be having problems with the code at the following line.

var obj = getObject(inObj);



error= object expected

Any help would be appreciated

Jon Hanlon
03-05-2002, 04:33 PM
Whoops!! Sorry about that!

getObject is a function of mine to convert a string to an object - we don't need it here.

So we'll just change inObj to be obj and she'll be apples:


function getSetRadio(obj) { // P2 is optional and implies we are Setting
// var obj = getObject(inObj); // remove this line
// if (!obj) return false; // and this line
for (var i=0; i < obj.length; i++) {
if (arguments.length > 1) { // we are setting
if (arguments[1] == null) {
obj[i].checked = false;
} else
if (obj[i].value == arguments[1]) {
obj[i].checked = true;
return true;
}
} else { // we are getting
if (obj[i].checked) return obj[i].value;
}
}
return false;
}
</script>


If you're really interested, I have previously posted the getObject function. If you use the Forum's search function you should find it.

Flussig
03-06-2002, 06:56 AM
Thanks very much. I have amended the code and it seems to work fine. Will let you know how i get on with adding it my code.

Cheers
Scott

Flussig
03-06-2002, 09:57 AM
I have added the code to my program, but there appears to be no validation on the radio box. Here is the complete program

Jon Hanlon
03-06-2002, 09:00 PM
The return statement does just that - it goes away.
No statements are executed after a return.
Also, your quotes are all wrong.

change

onsubmit="return ValidateForm();", "return ensureRadio (this)"


to be

onsubmit="return (ValidateForm() && ensureRadio(this))"

Flussig
03-08-2002, 11:12 AM
Thanks jon will let you know how i got on