...

View Full Version : Enable submit button when correct radio is selected



cvorachard
06-02-2005, 05:25 PM
Hi all.

New to JS and needing a little assistance. I have the following JS function:

function validateRadio(obj,correct){
frm=document.forms[0]
var result = 0
for(var i=0; i<obj.length; i++){
if(obj[i].checked==true && obj[i].value==correct) result = 1
}

if(!result && obj.value == correct) result = 1

return result
}

and in the body I have the following radio buttons:

<input name = "a" value="Warranty" onclick="if(validateRadio(this,'Warranty')) alert('Correct. You may proceed.')" type="radio">Warranty
<input name = "a" value="Durability" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect')" type="radio">Durability
<input name = "a" value="Cost" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect')" type="radio">Cost
<input name = "a" value="Because" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect')" type="radio">Because

with the html button:

<INPUT TYPE=button VALUE="Next" NAME="nButton">

On loading the page, I am disabling the button. Once the correct radio button is selected by the user, and I alert them the answer is correct, I would like to enable the Next button. If the answer is incorrect, keep it disabled.

Any suggestions?

TIA,
Clint

cvorachard
06-02-2005, 07:55 PM
changed it to the following and it works perfect:

<input name = "a" value="Warranty" onclick="if(validateRadio(this,'Warranty')) alert('Correct. You may proceed.');frm.nButton.disabled=false" type="radio">Warranty
<input name = "a" value="Durability" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');frm.nButton.disabled=true" type="radio">Durability
<input name = "a" value="Cost" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');frm.nButton.disabled=true" type="radio">Cost
<input name = "a" value="Because" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');frm.nButton.disabled=true" type="radio">Because

ImperialSpider
06-03-2005, 05:43 AM
I like this script, and I would like to know if you can take it a step further, by just getting rid of the 'Next' button all-together and make the 'OK' on the correct alert send the user to the next page/question? Any suggestions on how to re-do this code?

Thanks,
Imperial Spider



<script language="javascript/text">

function validateRadio(obj,correct){
frm=document.forms[0]
var result = 0
for(var i=0; i<obj.length; i++){
if(obj[i].checked==true && obj[i].value==correct) result = 1
}

if(!result && obj.value == correct) result = 1

return result
}

</script>

What do you get when you buy a brand new car?<p>

<input name = "a1" value="Warranty" onclick="if(validateRadio(this,'Warranty')) alert('Correct. You may proceed.');" type="radio">Warranty

<input name = "a2" value="Golfing skills" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');" type="radio">Golfing Skills

<input name = "a3" value="A new Pet" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');" type="radio">A New Pet

<input name = "a4" value="Super Powers" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');" type="radio">Super Powers

<p>

ImperialSpider
06-03-2005, 05:56 AM
<script language="javascript">

function validateRadio(obj,correct){
frm=document.forms[0]
var result = 0
for(var i=0; i<obj.length; i++){
if(obj[i].checked==true && obj[i].value==correct) result = 1
}

if(!result && obj.value == correct) result = 1

return result
}

</script>

What do you get when you buy a brand new car?<p>
<input name = "a" value="Warranty" onclick="if(validateRadio(this,'Warranty')) alert('Correct. You may proceed.');location='page2.html'" type="radio">Warranty
<input name = "a" value="Golfing skills" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');" type="radio">Golfing Skills
<input name = "a" value="A new Pet" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');" type="radio">A New Pet
<input name = "a" value="Super Powers" onclick="if(!validateRadio(this,'Warranty')) alert('Incorrect');" type="radio">Super Powers
<p>




Thanks

Imperial Spider



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum