lcy Posted October 20, 2009 Share Posted October 20, 2009 Hi all. I have some problems with radio buttons. Hope to get some help here. Thanks in advance. Here's the html: <?php while($row=mysql_fetch_array($result)) { ?> <input name="replacement" id="replacement" type="radio" value="<?php echo $row['date']; echo $row['title']; ?>" I need to validate if none of the radio buttons is checked using javascript. How can I do that? Any help will be appreciated. =) Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 20, 2009 Share Posted October 20, 2009 Hi Icy, This would have been better posted in the Javascript forum but click here for a good tutorial on using Javascript to validate radio buttons. Hope this helps. Quote Link to comment Share on other sites More sharing options...
lcy Posted October 20, 2009 Author Share Posted October 20, 2009 Sorry...because I'm using php for my system. And the javascript is just for validation part...so I posted it here. Sorry again... Quote Link to comment Share on other sites More sharing options...
lcy Posted October 20, 2009 Author Share Posted October 20, 2009 The link provided is not helping. The number of radio button here is not certain, depending on the result looped from database. I've tried something else using codes below. However, the message box displaying error pop out whenever I do not check the first radio button, which mean that I can just choose the first option. function check(){ var replacement = document.getElementById('replacement'); if (!replacement.checked) { alert("Please fill in all the fields marked with *"); replacement.focus(); return false; } } This code is working actually. If anyone here know how can I modify this codes to make it works better, your help is appreciated? Thanks. Quote Link to comment Share on other sites More sharing options...
lcy Posted October 21, 2009 Author Share Posted October 21, 2009 Solved! Codes used as below function validateCB(theName){ var counter=0; var cb=document.getElementsByName(theName) for (i=0; i<cb.length; i++) { if((cb[i].tagName=='INPUT')&&(cb[i].type=='radio')){ if (cb[i].checked) counter++; } } if (counter==0) { return false; } return true; } function check(){ if (!validateCB('replacement')) { alert("Please fill in all the fields marked with *"); return false; } return true; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.