harkly Posted June 15, 2010 Share Posted June 15, 2010 Can someone help me with my checkform?? I am trying to check if one of 2 radio buttons has been checked, if neither one is checked I want an alert stating so. THis is the code I am trying to modify. <script type="text/javascript"> function checkRadio(field) { for(var i=0; i < field.length; i++) { if(field[i].checked) return field[i].value; } return false; } function checkForm(form) { if(radioValue = checkRadio(form.gender)) { alert("You selected " + radioValue); return true; } else { alert("Error: No value was selected!"); return false; } } </script> I thought that if I did this it would work but of course it doesn't, been playing around with it for way to long <script type="text/javascript"> function checkRadio(field) { for(var i=0; i < field.length; i++) { if(field[i].checked) return field[i].value; } return false; } function checkForm(form) { if(radioValue != checkRadio(form.gender)) { alert("Error: No value was selected!"); return false; } return true; } } </script> Radio's on my form:: <br>Gender: <input type='radio' id='male' name='gender' value='2' class='outline'> Male <input type='radio' id='female' name='gender' value='1' class='outline'> Female Link to comment https://forums.phpfreaks.com/topic/204883-checkform-not-working/ Share on other sites More sharing options...
lemmin Posted June 15, 2010 Share Posted June 15, 2010 You are misunderstanding this line: if(radioValue = checkRadio(form.gender)) This is setting the variable 'radioValue' equal to whatever checkRadio() returns and then the if statement checks if it is true or false. Since you changed it to this: if(radioValue != checkRadio(form.gender)) You are checking if the return value of checkRadio() is NOT EQUAL to a variable 'radioValue' that, as far as I can tell, isn't even defined anywhere so it will always be false and ultimately return true. If you simply change it to this it should work: if(!checkRadio(form.gender)) If that is confusing, take a look at this simple form as a concept: <html> <head> <script type="text/javascript"> function checkForm(frm) { for (var i=0;i<frm.gender.length;i++) { if (frm.gender.item(i).checked) return true } alert("Gender is not selected"); return false; } </script> </head> <body> <form onsubmit="return checkForm(this)"> <input type="radio" name="gender" value="0" /> Male <input type="radio" name="gender" value="1" /> Female <input type="submit" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/204883-checkform-not-working/#findComment-1072610 Share on other sites More sharing options...
harkly Posted June 15, 2010 Author Share Posted June 15, 2010 Thanks lemmin! I thought that was my problem - the understanding part. Thanks for the clarification, got it to work! Link to comment https://forums.phpfreaks.com/topic/204883-checkform-not-working/#findComment-1072676 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.